Logging Module in Python

  • January 22, 2023
  • python
JOIN Clause
What is Logging?

Logging is a method of recording events that occur when software is run. Logging is essential for the development, debugging, and operation of software. If you don’t have a logging record and your programme crashes, there’s a slim chance you’ll find out what went wrong. And determining the cause will take a significant amount of time. Logging allows you to leave a breadcrumb trail so that if something goes wrong, we can pinpoint the source of the problem.

There are a variety of scenarios, such as expecting an integer but being given a float, using a cloud API while the service is down for maintenance, and so on. Such issues are out of hand and difficult to manage.

Python Logging Module:

The Python logging module is a ready-to-use and powerful module that is intended to meet the needs of both beginners and enterprise teams. It is used by the majority of third-party Python libraries, so you can combine your log messages with those from those libraries to create a unified log for your application.

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

import logging
Levels of Log Message:

The five built-in levels of log messages in the Python logging module are:

  1. DEBUG: Detailed information, typically of interest only when diagnosing problems.
  2. INFO: Confirmation that things are working as expected.
  3. WARNING: Indication that something unexpected happened, or is indicative of some problem in the near future.
  4. ERROR: Due to a more serious problem, the software has not been able to perform some functions.
  5. CRITICAL: Serious error, indicating that the program itself may be unable to continue running.

When configuring the logging system, you can specify the level of messages that should be logged by setting the level parameter of the basicConfig() function. For example, if you set the level to logging.WARNING, only messages with a level of WARNING, ERROR, and CRITICAL will be logged.

It’s also possible to create custom log levels, by using constants from logging module. These levels are in between the predefined levels and can be used to provide more detailed information about the log messages.

Python Logging Methods:

The Python logging module provides several methods for logging messages. The most commonly used methods are:

  • debug(): Logs a message with level DEBUG.
  • info(): Logs a message with level INFO.
  • warning(): Logs a message with level WARNING.
  • error(): Logs a message with level ERROR.
  • critical(): Logs a message with level CRITICAL.

Here’s an example of how to use these methods:

import logging

                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logging.debug('This is a debug message')
logging.info('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')


023-01-22 18:20:29,386 - root - DEBUG - This is a debug message
2023-01-22 18:20:29,386 - root - INFO - This is an info message
2023-01-22 18:20:29,386 - root - WARNING - This is a warning message
2023-01-22 18:20:29,386 - root - ERROR - This is an error message
2023-01-22 18:20:29,386 - root - CRITICAL - This is a critical message

Additionally, logging module also provides a method log() which is a general-purpose logging method that can log messages of any severity level.

logging.log(logging.DEBUG, 'This is a debug message')

It’s also possible to use the basicConfig method to configure logging to different handlers, that can output logs to different destinations such as file, remote server, etc.

It’s also possible to create custom logging methods by creating logger instances and specifying the level, handlers and formatters for it.

Note: also read about Python Shallow and Deep Copy

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.



Leave a Reply

Your email address will not be published. Required fields are marked *