Logging in Python Using Best Practices

Logging is a very important yet underused tool. Over the past couple of years, I have experienced that how logging has helped me to manage the applications in a better way, allowed me to easily communicate the code to other developers and to debug issues quickly!

This post will help you to understand how logging is incredibly helpful to a programmer and should be a practice.

What is Logging?

Logging is the process to capture the flow of the code. It is like breadcrumbs which will lead you to the cause of the issue in case of a program crash.

What I mean by logging in an application can be understood by below example-

When a function starts to execute, all the passed arguments value can be logged, the time when the function starts to execute and the return value of function.

By doing above logging, we can easily identify where things went wrong in case of a program crash or a bug. It also helps in investigating customer-reported issues.

Why Logging is beneficial?

Most of the people start their first program with print(“Hello World” ) which is the quickest way to check whether your code works or not, and then we all develop a habit to log the variables , execution time or important things in the program by using print statement only.

This might work for the small programs , but problem will arise when you use this approach on bigger projects which have multiple modules and complex architecture.

Now the question arises Why?

The logs which you require while debugging and development can be very different with the logs you require while the code is in production. You don’t want messy code with so many print statements to pass to production. Also logging has a lot of other features over print statements apart from just structured pattern-

  • gives module name which the message is coming from
  • gives control where to store the logs
  • gives control to differentiate the logs on basis of severity.

Without any delay, now lets jump right into it to see what all the fuss is about!

Logging module in Python

Python comes with a built-in logging module. This is part of standard python library, so there is no need to install it.

So, instead of using print statement just import logging module like this-

import logging

Below is a simple example of logging-

logger.info('Reading database')
data = {'Steve': 55, 'Harry': 66}
logger.debug('Records: %s', data)
logger.info('Updating records ...')
logger.info('Finished updating records')
logger.warning("I am a warning. Be careful")

Python Logging Levels

There are 5 standard levels of logging on the basis of severity-

  1. DEBUG: debug information, for troubleshooting. Value=10.
  2. INFO: Confirm things are working as expectation. Value=20.
  3. WARNING: Not an error, but pay attention. Value=30.
  4. ERROR: serious issue, but recoverable. Value=40
  5. CRITICAL: A serious error, unable to execute. Value=50

You have the control to configure the level you want to log in program. By default, the logging module logs the information with a severity level of WARNING or above.

Level can be configured like this-

logging.basicConfig(level=logging.INFO)

You can store all the python logging to file by using below configuration.

import logging
logging.basicConfig(level=logging.INFO, file='app_logs.log',filemode='w')

Best Practices

  • Use logging instead of print. Print might be easy to use, but it will give you headache as soon as the application grows.
  • Use levels as per requirement. It might be not easy to decide, but as explained in above section, you can easily decide when to use what.
  • Add a timestamp for each log. This action will benefit you in case you use logs to debug some issue. You can also identify performance issues if any.
  • Use RotatingFileHandler Class. Make sure that you use log rotation. This is to automatically archive, compress, or delete old log files to prevent full disks. Just use the RotatingFileHandler class instead of the regular FileHandler one.

What we learnt today?

Logging is essential and beneficial for most of the applications today as most of them are on cloud also it is demanded by many clients also. So, it is very important to learn logging and the best practices for all the developers out there.

So, if you have not used it till now, start today.

Happy learning 🙂

7 thoughts on “Logging in Python Using Best Practices

Leave a comment