Sitemap
TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Programming

Introduction to Logging in Python

How to log messages in Python and avoid print statements?

3 min readJul 3, 2020

--

Image by xresch from Pixabay

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 logging

Logging 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 message

This 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 message

In 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 message

Change 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().

Get Jimit Dholakia’s stories in your inbox

Join Medium for free to get updates from this writer.

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 message

The 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 message

The 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 occured

Example-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 zero

Example-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 zero

Conclusion

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.

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.