Python - Logging

The term "logging" refers to the mechanism of recording different intermediate events in a certain process. Recording logs in a software application proves helpful for the developer in debugging and tracing any errors in the application logic. Python's standard library includes logging module with which application logs can be generated and recorded.

It is a normal practice to use print() statements intermittently in a program to check intermediate values of different variables and objects. It helps the developer to verify if the program is behaving as per expectation or not. However, logging is more beneficial than the intermittent print statements as it gives more insight into the events.

Logging L evels

One of the important features of logging is that you can generate log message of different severity levels. The logging module defines following levels with their values.

Level When it's used Value


Detailed information, typically of interest only when diagnosing problems.



Confirmation that things are working as expected.



An indication that something unexpected happened, or indicative of some problem in the near future (e.g. 'disk space low'). The software is still working as expected.



Due to a more serious problem, the software has not been able to perform some function.



A serious error, indicating that the program itself may be unable to continue running.



The following code illustrates how to generate logging messages.

import logging

logging.debug('This is a debug message')'This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

It will produce the following output

WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message

Note that only the log messages after the WARNING level are displayed here. That is because root - the default logger ignores all severity levels above WARNING severity level. Notice severity level is logged before the first colons (:) of each line. Similarly, root is the name of the logger is also displayed the LogRecord.

Logging Configuration

The logs generated by the program can be customized with BasicConfig() method. You can define one or more of the following parameters for configuration −

  • filename − Specifies that a FileHandler be created, using the specified filename, rather than a StreamHandler.

  • filemode − If filename is specified, open the file in this mode. Defaults to 'a'.

  • datefmt − Use the specified date/time format, as accepted by time.strftime().

  • style − If format is specified, use this style for the format string. One of '%', '{' or '$' for printf-style, str.format() or string.Template respectively. Defaults to '%'.

  • level − Set the root logger level to the specified level.

  • errors − If this keyword argument is specified along with filename, its value is used when the FileHandler is created, and thus used when opening the output file. If not specified, the value 'backslashreplace' is used. Note that if None is specified, it will be passed as such to open(), which means that it will be treated the same as passing 'errors'.


To log all the messages above DEBUG level, set the level parameter to logging.DEBUG

import logging

logging.debug('This message will get logged')

It will produce the following output

DEBUG:root:This message will get logged

To record the logging messages in a file instead of echoing them on the console, use filename parameter.

import logging

logging.basicConfig(filename='logs.txt', filemode='w', level=logging.DEBUG)
logging.warning('This messagewill be saved to a file')

No output will be displayed on the console. However, a logs.txt file is created in current directory with the text WARNING:root:This message will be saved to a file in it.

Variable Data in L ogging M essage

More often than not, you would like to include values of one or more variables in the logging messages to gain more insight into the cause especially of errors generated while the application is running. To do that, any of the dynamic string formatting techniques such as format() method of str class, or f-strings can be used.


import logging

marks = 120
logging.error("Invalid marks:{} Marks must be between 0 to 100".format(marks))
subjects = ["Phy", "Maths"]
logging.warning("Number of subjects: {}. Should be at least three".format(len(subjects)))

It will produce the following output

ERROR:root:Invalid marks:120 Marks must be between 0 to 100
WARNING:root:Number of subjects: 2. Should be at least three
Kickstart Your Career

Get certified by completing the course

Get Started