Logging in Python

Logging is a very helpful tool in a programmer’s toolkit. We can learn more about a program’s flow and come across edge cases that we might have missed during development.

Python has a built-in package, logging, which allows writing status messages to log files or other output streams. The log file helps to determine the cause when debugging an error in the code.

In this answer, we are covering log levels of the Python logging module.

Logs Levels

There are 5 different log levels, we’ll go through them in increasing order of severity.

  1. Debug - Statements that are only displayed when the debug mode is on, which helps to debug issues.

Example:
Debug - Imported Pandas module

  1. Info - Statements that would normally go into the print statements. These record/imitate the status of the program running.

Example:
Info - “Connection successfully established!”

  1. Warning - Statements that will indicate possible failures of the program, but will not change the expected outcome.

Example:
Warning - This version of module - 1.01 has been deprecated. Please use version 1.02"

  1. Error - Statement that hinders the runtime execution of the program.

Example:
Error - Failed to establish connection. Rolling back to previous snapshot 1.4"

  1. Critical - Statements that indicate a breakdown of the program and need immediate attention from the developer.

Example:
Critical - Failed to establish a connection. Rolling back to the previous snapshot 1.4 FAILED. Exiting program"

Code

Next up! There are some powerful modules that deal with logging in python. In this shot, we’ll look at the logging module from python. Its simple to use the logging module:

import logging

logging.debug("debug statement")
logging.info("info statement")
logging.warning("warning statement")
logging.error("error statement")
logging.critical("critical statement")

But, for a more pythonic way, use the basicConfig() function like this:

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('Define your debug statement here')

Next, if you are working on huge server workloads, I would suggest using the log handlers from python. Here is a snippet that will help you to write the logs to a different file after it reaches a max capacity of 20 bytes.

import logging
from logging.handlers import RotatingFileHandler


logger = logging.getLogger("Rotating Log")

log_handler = RotatingFileHandler(path, maxBytes=20, backupCount=5)
logger.addHandler(log_handler)

Until next time! Cheers. :)

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved