In this post we will see Python's basic logger. Most of us use Print statements, though it is useful to log an error when occurred or expected to test certain working module during unit testing. But it is best to use logger when working with small and real world project as it has huge advantage over print statement.
In python logger comes in-build, so we don't have to install any additional package to work with them. Logger is lot to cover so we have divided this topic into two post, this post covering basics and next post covering advanced practices. Lets get started,
There are 5 types of logging level
DEBUG : These are used to give Detailed information, typically of interest only when diagnosing problems.
INFO : These are used to Confirm that things are working as expected
WARNING : These are used an indication that something unexpected happened, or indicative of some problem in the near future
ERROR : This tells that due to a more serious problem, the software has not been able to perform some function
CRITICAL : This tells serious error, indicating that the program itself may be unable to continue running
Below is the simple program, which uses simple print statements
IN[1]
# Add function
def add(x, y):
return x + y
# Subtract function
def subtract(x, y):
return x - y
# Multiple function
def multiple(x, y):
return x * y
# Divide function
def divide(x, y):
return x / y
# Varaibale
num_1 = 10
num_2 = 5
# Function call
add_result = add(num_1, num_2)
print(f'Add: {num_1} + {num_1} = {add_result}')
subtract_result = subtract(num_1, num_2)
print(f'Subtract: {num_1} + {num_1} = {subtract_result}')
multiple_result = multiple(num_1, num_2)
print(f'Multiple: {num_1} + {num_1} = {multiple_result}')
divide_result = divide(num_1, num_2)
print(f'Divide: {num_1} + {num_1} = {divide_result}')
OUT[1]
P