Logging in Python

Aditya Mahamuni
3 min readDec 23, 2022

--

Logging in Python

Logging is a method of recording events that occur when software is run. The software developers adds logging calls to their code to indicate that certain events have occurred. Events also have an importance which the developer attributes to the event; the importance can also be called the level or severity.

The Python logging module is a built-in package that allows for customizable message logging. It allows you to record messages of varying severity (for example, debug, info, warning, error, critical) and to define the destination of the logged messages (for example, console, file, network).

The logging module has a modular approach and it provides various components:

  • Loggers — Exposes the interface that the code directly uses.
  • Handlers — Sends the log messages and records which are created by the loggers to the appropriate destination (i.e., console, file, etc.)
  • Filters — Provides a granular control and facility for determining how and which logs to record to the output.
  • Formatters — Specifies the layout of log messages and records in the final log report.

Logging is carried out by calling methods on the logger class instances. To use the logging module, you first need to import it and configure a logger. Here is an example of how to do this:

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

In this example, the ‘basicConfig’ function is used to configure the logger with a log level of ‘DEBUG’ and a log format that includes the time, log-level and the message. The ‘getLogger’ function is used to create a logger with the specified name.

A good convention to use when naming loggers is to use a module-level logger, in each module which use logging module, which can be as shown in the example :

logger = logging.getLogger(__name__)

Once you have configured a logger, you can use it to log messages to various levels of severity. For example:

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

By default, the logging module sends the logged messages to the console. However, one can also configure it to log messages to a file or to a network destination.

For example, to log messages to a file, you can use the ‘FileHandler’ class provided by logging module.

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Create a FileHandler
handler = logging.FileHandler('logfile_example.txt')
handler.setLevel(logging.DEBUG)

# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# Add the FileHandler to the logger
logger.addHandler(handler)

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

In this example, a file handler is created and added to the logger instance. Subsequently a logging format is specified for the handler. All logged messages will be written to the ‘logfile_example.txt’.

The logging functions are named for the severity or degree of the events they track. The following are the standard levels and their application (in increasing order of severity):

  • DEBUG— Detailed information, typically of interest only when diagnosing problems.
  • INFO — Confirmation that things are working as expected.
  • WARNING — An indication that something unexpected happened, or indicative of some problem in the near future. The software is still working as expected.
  • ERROR — Due to a more serious problem, the software has not been able to perform some function.
  • CRITICAL — A serious error, indicating that the program itself may be unable to continue running.

The default level is WARNING, which indicates that only events of this level and above this level will be tracked, unless the logging instance is configured to handle other levels.

The Python logging module is a versatile and effective tool for logging messages in Python applications. It allows you to determine the severity of logged messages as well as their destination, and it offers several options for modifying the log format and processing log messages.

There’s a lot more that the logging module offers, but to get the most out of it, you will need to invest a little more of your time in reading the advanced/in-depth tutorial than the basic one covered above.

You can take a look at the Logging Cookbook for more advanced and in-depth material on logging module.

--

--

Aditya Mahamuni
Aditya Mahamuni

No responses yet