Logging Levels in Python

Logging Levels in Python

What is logging?

Logging is a very important tool in a programmer’s toolbox that enables your code to record events as the program executes for later analysis.

The application that contains log messages provides developers extra eyes to understand the system flow in different ways.

Logs can store user information like timestamp, IP, location during the login time of a particular app.

 

Why use logging?

  • A useful debugging feature that captures and records events while the app is running.
  • Always records data that is more or less important that brings insight into how things are functioning.
  • Provide a highly customizable output.
  • Provide transaction records of a program’s execution.

 

Logging in Python

Python as the most popular programming language supports logging features diligently. Its built-in logging module is a powerful module that fulfills all the needs of beginners as well as enterprise teams.

Logging module is used by almost all the third-party libraries to log messages and you can use your own logs to produce a homogeneous for your app.

 

Logging Levels

Python logging module by default has five standard different levels/methods to use for recording log messages. Such levels indicate the different types of status of the application.

  1. DEBUG
  2. INFO
  3. WARNING
  4. ERROR
  5. CRITICAL

 

1. DEBUG: It is typically used to provide diagnostic information that is useful when we are tracking down a problem while debugging.

2. INFO: It is used to indicate that a particularly interesting operation was able to execute normally i.e. general information about program execution results.

3. WARNING: It is used to log something unexpected, or an approaching problem. Such as running out of storage space or the inability to communicate with the remote server.

4. ERROR: It is used to log error messages in the condition that the app is unable to perform a specific operation due to a problem.

5. CRITICAL: It is used to indicate that the program may not be able to continue due to serious errors.

Example

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:

WARNING:root:This is a WARNING message
ERROR:root:This is an ERROR message
CRITICAL:root:This is a CRITICAL message

In the above output, debug() and info() messages didn’t get logged because, by default, the logging module logs the messages with a severity level of WARNING or above.

But if you want to log the messages from DEBUG level, then set the level=logging.DEBUG and pass inside the basicConfig function of logging module as:

import logging

# configure to show logs from DEBUG level
logging.basicConfig(level=logging.DEBUG)

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:

DEBUG:root:This is a DEBUG message
INFO:root:This is an INFO message
WARNING:root:This is a WARNING message
ERROR:root:This is an ERROR message
CRITICAL:root:This is a CRITICAL message

Leave a Reply