Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python3
server
logs
community creator

Logging in Python

Nikhil JSK

Logs are very important when debugging any issue. So, how do you do it in Python correctly?

First off, there are multiple log levels that you need to know.

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. :)

RELATED TAGS

python3
server
logs
community creator
RELATED COURSES

View all Courses

Keep Exploring