Post
1699
Are you a Professional Python Developer? Here is why Logging is important for debugging, tracking and monitoring the code
Logging
Logging is very important part of any project you start. It help you to track the execution of a program, debug issues, monitor system performance and keep an audit trail of events.
Basic Logging Setup
The basic way to add logging to a Python code is by using the logging.basicConfig() function. This function set up basic configuration for logging messages to either console or to a file.
Here is how we can use basic console logging
Logging to a File
We can also save the log to a file instead of console. For this, we can add the filename parameter to logging.basicConfig().
You can read more on my medium blog https://medium.com/@imranzaman-5202/are-you-a-professional-python-developer-8596e2b2edaa
Logging
Logging is very important part of any project you start. It help you to track the execution of a program, debug issues, monitor system performance and keep an audit trail of events.
Basic Logging Setup
The basic way to add logging to a Python code is by using the logging.basicConfig() function. This function set up basic configuration for logging messages to either console or to a file.
Here is how we can use basic console logging
#Call built in library
import logging
# lets call library and start logging
logging.basicConfig(level=logging.DEBUG) #you can add more format specifier
# It will show on the console since we did not added filename to save logs
logging.debug('Here we go for debug message')
logging.info('Here we go for info message')
logging.warning('Here we go for warning message')
logging.error('Here we go for error message')
logging.critical('Here we go for critical message')
#Note:
# If you want to add anything in the log then do like this way
records=100
logging.debug('There are total %s number of records.', records)
# same like string format
lost=20
logging.debug('There are total %s number of records from which %s are lost', records, lost)
Logging to a File
We can also save the log to a file instead of console. For this, we can add the filename parameter to logging.basicConfig().
import logging
# Saving the log to a file. The logs will be written to app.log
logging.basicConfig(filename='app.log', level=logging.DEBUG)
logging.debug('Here we go for debug message')
logging.info('Here we go for info message')
logging.warning('Here we go for warning message')
logging.error('Here we go for error message')
logging.critical('Here we go for critical message')
You can read more on my medium blog https://medium.com/@imranzaman-5202/are-you-a-professional-python-developer-8596e2b2edaa