Skip to content

Commit b31cafb

Browse files
committed
Enbale logging in python code
1 parent ba9f603 commit b31cafb

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

python_logging.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Way to add different logging levels in python program
2+
# Python library to enable logging
3+
import logging
4+
5+
# it provides four levels of logging
6+
# DEBUG -- used for debugging or diagnosis
7+
# INFO -- Confirmation of things working as expected
8+
# WARNING -- Default Level. Indication of something unexpected happened but program works fine
9+
# ERROR -- Due to serious problem software is not able to perform well
10+
# CRITICAL -- Serious error happened, program is not able to run
11+
12+
# To change default logging level
13+
#logging.basicConfig(level=logging.DEBUG)
14+
15+
# To write to file
16+
#logging.basicConfig(filename='test.log', level=logging.DEBUG)
17+
18+
# TO add format to logging
19+
logging.basicConfig(filename='test.log', level=logging.DEBUG, format='%(asctime)s:%(levelno)s:%(message)s')
20+
21+
def add(num1, num2):
22+
return num1 + num2
23+
24+
def subtract(num1, num2):
25+
return num1 - num2
26+
27+
def multi(num1, num2):
28+
return num1 * num2
29+
30+
def divide(num1, num2):
31+
return num1 / num2
32+
33+
34+
num1 = 20
35+
num2 = 4
36+
37+
38+
add_result = add(num1, num2)
39+
#print("Add: {} + {} := {}". format(num1, num2, add_result))
40+
logging.debug("Add: {} + {} := {}". format(num1, num2, add_result))
41+
42+
sub_result = subtract(num1, num2)
43+
#print("Subtract: {} + {} := {}". format(num1, num2, sub_result))
44+
logging.debug("Subtract: {} + {} := {}". format(num1, num2, sub_result))
45+
46+
mul_result = multi(num1, num2)
47+
#print("Multiply: {} + {} := {}". format(num1, num2, mul_result))
48+
logging.debug("Multiply: {} + {} := {}". format(num1, num2, mul_result))
49+
50+
div_result = divide(num1, num2)
51+
#print("Subtract: {} + {} := {}". format(num1, num2, div_result))
52+
logging.debug("Subtract: {} + {} := {}". format(num1, num2, div_result))

test.log

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
DEBUG:root:Add: 10 + 3 := 13
2+
DEBUG:root:Subtract: 10 + 3 := 7
3+
DEBUG:root:Multiply: 10 + 3 := 30
4+
DEBUG:root:Subtract: 10 + 3 := 3.3333333333333335
5+
DEBUG:root:Add: 20 + 4 := 24
6+
DEBUG:root:Subtract: 20 + 4 := 16
7+
DEBUG:root:Multiply: 20 + 4 := 80
8+
DEBUG:root:Subtract: 20 + 4 := 5.0
9+
DEBUG:root:Add: 20 + 4 := 24
10+
DEBUG:root:Subtract: 20 + 4 := 16
11+
DEBUG:root:Multiply: 20 + 4 := 80
12+
DEBUG:root:Subtract: 20 + 4 := 5.0
13+
2018-11-28 18:04:23,212:10:Add: 20 + 4 := 24
14+
2018-11-28 18:04:23,213:10:Subtract: 20 + 4 := 16
15+
2018-11-28 18:04:23,213:10:Multiply: 20 + 4 := 80
16+
2018-11-28 18:04:23,213:10:Subtract: 20 + 4 := 5.0

0 commit comments

Comments
 (0)