Over a million developers have joined DZone.

Using Logging in Python for Better Reporting

DZone's Guide to

Using Logging in Python for Better Reporting

In this article, we show you a few commands, and give a real-life use case, that can help you use Python to make logging a little easier.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

When moving into an Agile/DevOps model of code production, one of the core concepts is building in enough monitoring and telemetry. To enable this, most programming languages come with bundled libraries for logging. There is log4J in Java and logging library in Python. In this (short) post, I walk through a use case of how it helped.

Image title

Nikon RAW Images

I am trying to be a better digital photographer and the first step was to enable shooting in RAW mode. Since I am still new, I shoot in a combined mode. My camera produces a raw (.NEF) and a jpeg (.jpg) image for every shot. When I open the images, it's easy to view the photos and I delete the obviously bad ones from within the tool. So this has a challenge of deleted .JPG files but orphaned .NEF files.

So I wrote a script to clean up such files. Here's the code: https://github.com/akshayranganath/FileCleaner.

However, in my first version of the code, I wasn't sure on how best to display the messages. For example:

  • Should I print the name of all the files that would be deleted?
  • Should I hide everything and just say "Done"?
  • How to signal there is an error rather than just a message?

Enter Logging

This is when I realized that logging would be a better solution. It offers varying levels of verbosity and an ability to express a message and tell the kind of message that it is. So I can have an informational message or a debug message or an error message. {For the full list, refer to Logging Levels}. When a user fires the code with a specific verbosity level, I can fine tune the amount of chattiness of the code.

So if I just want to get the work done, I can invoke this command:

python file_cleaner.py --folder /some/folder 

If I want to see all the messages including each file being deleted, I can use:

python file_cleaner.py --folder /some/folder -v 

I could even start to accept a specific verbosity level as a number between 0-100 and the messages would vary.

Why Is it Useful?

Here's why I think this is super-cool:

  • As a developer, we often put in print statements. We either comment or delete them as time goes by. However, we'd put this in because we'd been stuck at this point for some reason. By removing the comment, we take away this ability to debug. Instead of using a print, what if there was a logging.debug() message? This way, the message is never displayed in normal operations but printed out when necessary.
  • Logging levels could be externalized through an environment variable. This way, when code is deployed, the automation deploying the code could set the verbosity level depending on the complexity of the change and then tweak it over time. So all new code could start at the DEBUG level and then move to INFO and then to ERROR.

I am still learning the usages. So if you have other use cases, do put in a note!

{I had originally published the article here: Using Logging in Python for better reporting}

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

performance ,logging ,python

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}