|
| 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)) |
0 commit comments