Introduction to Logging in Python
How to log messages in Python and avoid print statements?
Logging is, perhaps, the most underrated aspect of any software development. Logging helps to develop robust programs by recording the events of the program. Logging is used for various purposes — from debugging to monitoring the application.
Python’s standard library provides a module for logging. In this article, I will show you how to add logging to your programs and develop better applications.
To use logging in your applications, import the logging module
import loggingLogging Levels
The logging module comes predefined with 5 levels of severity which are as follows:
┌──────────┬───────┐
│ Level │ Value │
├──────────┼───────┤
│ CRITICAL │ 50 │
│ ERROR │ 40 │
│ WARNING │ 30 │
│ INFO │ 20 │
│ DEBUG │ 10 │
└──────────┴───────┘The default level is WARNING, which means only the events of this level and above will be logged.
The logging module provides a set of functions for simple usage. These are shown in the example below:
import logging
logging.debug('Debug message')
logging.info('Info message')
logging.warning('Warning message')
logging.error('Error message')
logging.critical('Critical message')The output of the above example will be:
WARNING:root:Warning message
ERROR:root:Error message
CRITICAL:root:Critical messageThis is because the default logging level is set to WARNING.
Change Logging Level
The logging module provides a function to set the basic configuration for the logger.
To change the level of the logger, pass the level argument for basicConfig() .
Example:
import logging
logging.basicConfig(level=logging.INFO)
logging.debug('Debug message')
logging.info('Info message')
logging.error('Error message')The output of the above example would be:
INFO:root:Info message
ERROR:root:Error messageIn this example, the logging level is set to INFO. So the ‘Info message’ will be logged but the ‘Debug message’ won’t be logged since the level of INFO is greater than DEBUG.
Log to a file
To log the messages to a file, simply pass the name of the file in the filename parameter of the basicConfig()
Example:
import logging
logging.basicConfig(filename='sample.log', level=logging.INFO)
logging.debug('Debug message')
logging.info('Info message')
logging.error('Error message')The contents of the file would be:
INFO:root:Info message
ERROR:root:Error messageChange logging Format
The default logging format is %(levelname)s:%(name):%(message)s.
To change the default format, we need to specify the format parameter in the basicConfig().
Example:
FORMAT = '%(asctime)s:%(name)s:%(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logging.info('Info message')The corresponding output would be:
2020-07-03 00:48:00,106:root:INFO - Info messageThe complete list of formatting attributes can be found in the official documentation.
Change date format
To change the date format displayed in the logs, we need to change the datefmt parameter.
Example:
FORMAT = '%(asctime)s:%(name)s:%(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT,
level=logging.INFO,
datefmt='%Y-%b-%d %X%z')
logging.info('Info message')Output:
2020-Jul-03 00:56:31+0530:root:INFO - Info messageThe list of available date formats can be found in the documentation.
Logging for exceptions
To log the trace of the exception, you can either use logging.exception or use logging.error with exc_info=True.
Example-1: with logging.error
try:
5/0
except:
logging.error('Exception occured')Output-1:
ERROR:root:Exception occuredExample-2: with logging.error and exc_info=True
try:
5/0
except:
logging.error('Exception occured', exc_info=True)Output-2:
ERROR:root:Exception occured
Traceback (most recent call last):
File "<ipython-input-2-933e0f6b1879>", line 11, in <module>
5/0
ZeroDivisionError: division by zeroExample-3: with logging.exception
try:
5/0
except:
logging.exception('Exception occured')Output-3:
ERROR:root:Exception occured
Traceback (most recent call last):
File "<ipython-input-3-e7d1d57e6056>", line 11, in <module>
5/0
ZeroDivisionError: division by zeroConclusion
I hope you were able to understand the basics of Logging in Python. Using this article, you can start using logging for any application for various purposes such as debugging, usage monitoring, and performance monitoring. You can refer to the official documentation for the complete list of available methods.
Resources
The code snippets used in this article are available on my GitHub page.
References
Let’s Connect
LinkedIn: https://www.linkedin.com/in/jimit105/
GitHub: https://github.com/jimit105
Twitter: https://twitter.com/jimit105

