Signup/Sign In

Print Logs in Python - Logging Module

In this tutorial, we will learn what is logging? (basics of logging), how we can print logs in python and the different log levels that should be used while printing logs.

First of all, let us discuss what is logging.

What is Logging?

Logging is basically a way to track events(what is happening) whenever any program/script runs. Logging calls are added by software developers in their software to print useful steps during software code execution along with some information which can be later used to track down the code execution.

  • Proper logging can help you develop a better understanding of the flow of the program and it is very helpful in discovering those scenarios which you may have not thought at about during development.

  • Logging is very in debugging issues.

  • With the help of Logs, developers get an extra set of eyes to look at the flow of the application. Logs can store information, like which user ID or IP which is accessing the application.

  • By logging useful data and metrics in the software application, you can not only debug errors easily but you can also use the data to analyze the performance of the application to plan for scaling.

  • An event is described as a descriptive message which can optionally contain variable data (i.e. data is mainly different for each occurrence of the event).

  • Events or Logs printed, also have an importance also known as Log level, which we will cover in the later section of this tutorial.

Many beginners use the print() method in their python code to print statements and variables, to debug their code.

Using logs to do this is the right approach.

Python Logging Module

The Logging module is an inbuilt module in Python which is powerful and ready to use. This module allows writing logs either to a file or console or to any other output stream.

This module is used by many third-party Python libraries. So if your python project uses many third party libraries, then you can use the logging module, so that log handling can be same in your project and the third party libraries you are using.

To use this module you just need to write the following:

import logging

After importing the logging module, you can use various methods provided by the logging module to print the logs.

Different Levels of Log Message

In the Logging Module, there are 5 standard levels by default that mainly indicate the severity of events. Also, the logging functions are named after the level.

The levels, in order of increasing severity, are given below:

python log level severity

Let us discuss the meaning of each level one by one in the table given below:

Level Time to use
DEBUG This level is mainly used to provide detailed information, typically of interest only when debugging problems.
INFO This level is used to confirm that things are working as expected. Just the usual information.
WARNING This level tells that something unexpected happened but not too severe that it may affect the normal functioning of the program/software.
ERROR This should be used to log more serious problems like errors or exceptions leading to functionality getting broken.
CRITICAL This level indicates a super serious error like the application not getting started or the database being unavailable to setup connection, etc.

You can all the types of log levels in your python code to log different information.

But what logs will be printed, depends on the Logging configuration.

It is important to note that the default level is WARNING, which means that only events of this level and above this level will be tracked, which are, WARNING, ERROR and CRITICAL.

Python Logging Methods

There are some convenient functions for simple logging usage like debug(), info(), warning(), error() , and critical(). Let us discuss them, one by one:

How to Log? Task to perform
print() method If you want to display normal messages on console for the user information.
warning.warn() and logging.warn()

To log a warning regarding a particular runtime event.

The warning.warn() method is used in python code if the issue is avoidable and the changes must be made to eliminate the warning.

The logging.warning() method is used in the case there is some small issue/error which doesn't have any direct impact on the functioning of the code, but must be tracked as it may cause problem later. For example, module version mismatch, etc.

Raise an Error/Exception In order to report an error regarding a particular runtime event.
logging.error() or logging.critical()

If you want to report the an error without raising an exception. If you have done proper exception handling in python, but you must still log the exception which is handled so that it can later be found and fixed.

logging.critical() can be used for critical errors like program startup failed or database connection failed, etc.

logging.info() or logging.debug()

The logging.info() method can be used in order to report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation).

logging.debug() is specifically used for very detailed output for diagnostic purposes.

Python Logging Basic Example

Let us take a basic example where we will print log messages corresponding to different log levels:

import logging

logging.debug('It is a debug message')	# it will not be printed
logging.info('It is an info message')	# not printed
logging.warning('OOPs!!! It is a warning')	# it will be print because it is default level
logging.error('Oops !! an error message')	# will be printed 
logging.critical('Oh!!!! it is a critical message')	# will be printed

WARNING:root: OOPs!!! It is a warning ERROR:root: Oops !! an error message CRITICAL:root: Oh!!!! it is a critical message

The above output shows the severity level just before each message along with root , which is the name that the logging module gives to its default logger.

The above output format shows the level, name, and message and all are separated by a colon (:) and it is the default format for logs(we can change the format).

It is important to note that debug() and info() messages didn't get logged. It is because, by default, the logging module logs the messages with a severity level of WARNING or above as we have already mentioned.

You can also change that by just configuring the logging module to log events of all levels if you want to do so.