Skip to content

Python logging module

  • by

The Python logging module is the facility to work with the framework for releasing log messages from the programs. This module is widely used by developers when they work on logging.

Adding logging to your Python program:

import logging

Python logging levels indicating the severity of events:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

Python logging module example

Simple example code.

import logging

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')

Output:

Python logging module

Basic Configurations

You can use the basicConfig(**kwargs) method to configure the logging:

  • level: The root logger will be set to the specified severity level.
  • filename: This specifies the file.
  • filemode: If filename is given, the file is opened in this mode. The default is a, which means append.
  • format: This is the format of the log message.

Read more: https://docs.python.org/3/library/logging.html

Do comment if you have any doubts or suggestions on this Python logging topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading