Introduction to Python Logging Module and Logging Levels

In this article, we will see the introduction to Python logging module and the different logging levels. While developing any software in python, the most important tool for any developer is logging. It helps the developer to understand the flow better and ends up discovering the scenarios which might not have got attention without using the logging module. In this tutorial we will walk through some basic functionality of logging module, different logging level of events and how to capture the logs in a file as well as print on console. Let’s get started.

 

Introduction to Python Logging Module and Logging Levels

What is Python Logging Module ?

Also Read: Braitenberg’s Dream: A tale of a brain is a tail of a body [Robotics Tutorial]

Python provides a built-in module called logging  which is used to track events that happens when some application runs. We generally store these events in a file or any other output streams. This is one of the most important and helpful tool for a developer.

We may encounter unexpected scenarios while developing a software. Through logging, it becomes quite helpful for a developer to debug and fix such scenarios. There are so many important aspect of a software that we can capture in log which not only helps in understanding the flow of execution but also the performance of software.

 

NOTE:

Please note that I am using root user to run all the below programs. You can use any user to run below programs. Also, I am using CentOS 7 with Python 2.7.5 installed.

 

Logging Levels

By default logging module provides 5 standard level of severity of events. These are:-

  • DEBUG: used to indicate detailed information of events  which can be useful to troubleshoot in case of any failure.
  • INFO: used to indicate success execution of events as expected.
  • WARNING: used to indicate some potential issue that may occur in nearby future but application still works as expected.
  • ERROR: used to indicate some function execution has failed due to some serious issue.
  • CRITICAL: used to indicate application execution itself has failed due to some critical error.

default level is WARNING which means all events of this level and above will be tracked with the default configuration. We can customize the level to track by changing the default configuration which we will discuss later in this tutorial. Each level has been assigned a numeric value. Developers have the option to create more logging levels based on the needs but in most cases below are the sufficient levels to capture any events.

LEVELNUMERIC VALUE
NOTSET0
DEBUG10
INFO20
WARNING30
ERROR40
CRITICAL50

 

Print Logging Events On Console

[root@linuxnasa ~]# vi demo.py
import logging

logging.debug("\nMore information of an event")
logging.info("Success message for an event")
logging.warning("Potential warning in nearby future")
logging.error("Something went wrong in some function execution")
logging.critical("Program execution error\n")

Save the file and execute as shown below.

Output

[root@linuxnasa ~]# python demo.py
WARNING:root:Potential warning in nearby future
ERROR:root:Something went wrong in some function execution
CRITICAL:root:Program execution error

Observe that it only prints the events at level WARNING or above that since that is the default configuration of logging module. Hence INFO and DEBUG events are not printed.

Next we will see how to capture events in a file. Also, we will see how to change the default logging level while capturing the events in a file.

 

Logging to a File

To capture the events with different severity in a file, we can use the default logger provided by the logging module. We call corresponding methods for each level to capture the events.  In below code example, we have imported the logging module along with other modules.

[root@linuxnasa ~]# vi demo.py
#import modules
import logging
from logging import DEBUG
from datetime import datetime
#log file path
logname = "/tmp/application.log"
#Create and configure logger
logging.basicConfig(filename=logname, level=logging.DEBUG, format='%(asctime)s :: %(levelname)s :: %(message)s')
#Create an object
logger = logging.getLogger("%s"%logname)
#Setting the level to INFO
logger.setLevel(logging.INFO)
#Messages to test
logger.debug("More information of an event")
logger.info("Success message for an event")
logger.warning("Potential warning in nearby future")
logger.error("Something went wrong in some function execution")
logger.critical("Program execution error")

Save the file and execute as shown below.

Output

[root@linuxnasa ~]# python demo.py
2023-01-18 19:08:50,989 :: INFO :: Success message for an event
2023-01-18 19:08:50,989 :: WARNING :: Potential warning in nearby future
2023-01-18 19:08:50,989 :: ERROR :: Something went wrong in some function execution
2023-01-18 19:08:50,989 :: CRITICAL :: Program execution error

As discussed above, we have set the threshold of logger to INFO. Hence all the events of this level and above will get logged in a file. Since the DEBUG level is lower than INFO, it did not get logged.

Leave a Comment