avatarOliver S

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1612

Abstract

class="hljs-keyword">import</span> logging

logging.debug(<span class="hljs-string">'DEBUG'</span>) logging.info(<span class="hljs-string">'INFO'</span>) logging.warning(<span class="hljs-string">'WARNING'</span>) logging.error(<span class="hljs-string">'ERROR'</span>) logging.critical(<span class="hljs-string">'CRITICAL'</span>)</pre></div><p id="26db">Upon running this, we should only see three outputs: WARNING / ERROR / CRITICAL.</p><p id="84d1">This is because the default log level is set to warning, i.e. everything below warning is not printed.</p><p id="27e0">To change that, we add a single line after the import:</p><div id="1571"><pre><span class="hljs-keyword">import</span> logging

logging.basicConfig(level=logging.DEBUG) logging.debug(<span class="hljs-string">'DEBUG'</span>) logging.info(<span class="hljs-string">'INFO'</span>) logging.warning(<span class="hljs-string">'WARNING'</span>) logging.error(<span class="hljs-string">'ERROR'</span>) logging.critical(<span class="hljs-string">'CRITICAL'</span>)</pre></div><p id="632c">… and now see all the output.</p><p id="5413">As teased in the beginning, there is a multitude of ways to customise logging behaviour. However, I’ll be brief and end this post here, as in my professional experience already these steps help in developing a scalable software architecture. Hope you enjoyed!</p><p id="e425">This post is part of a series show-casing important Python concepts quickly. You can find the other parts here:</p><ul><li>Part 1: <a href="https://readmedium.com/lambda-functions-in-python-a124cebc2e99">Lambda Functions in Python</a></l

Options

i><li>Part 2: <a href="https://readmedium.com/iterators-in-python-bd7332ddbc00">Iterators in Python</a></li><li>Part 3: <a href="https://readmedium.com/generators-and-generator-expressions-in-python-a8d2e700945e">Generators and Generator Expressions in Python</a></li><li>Part 4: <a href="https://readmedium.com/advanced-iteration-in-python-with-enumerate-and-zip-676b31ceac44">Advanced Iteration in Python with enumerate() and zip()</a></li><li>Part 5: <a href="https://readmedium.com/managing-resources-in-python-with-context-managers-with-statement-f07afc1afb4f">Managing Resources in Python with Context Managers (with statement)</a></li><li>Part 6: <a href="https://readmedium.com/generating-temporary-files-and-directories-in-python-dfc11f017a97">Generating Temporary Files and Directories in Python</a></li><li>Part 8: <a href="https://readmedium.com/partial-functions-in-python-66998eef1384">Partial Functions in Python</a></li><li>Part 9: <a href="https://readmedium.com/f-strings-in-python-80e30c64fb95">f-Strings in Python</a></li></ul><p id="b038"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="8efb"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>YouTube</b></a>, and <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p></article></body>

Logging in Python

Python Shorts — Part 7

Logging is a powerful introspection tool, and a must for any professional software project. With it, we can keep track of our application’s execution, follow progress, find bugs and overall improve debugging.

Luckily, Python offers a convenient standard library logging module, satisfying all these requirements.

Photo by Chris Ried on Unsplash

One good question to start with is: why use logging over simple prints? There are several reasons:

  • Many other Python modules use this standard library logging module, thus we also pick up their log prints along the way.
  • Logging offers much convenience functionality for the designated use-cases, such as automatically printing the line from where it was called, and supports different log levels (more below).
  • Logging offers many customisation options, such as formatting, sending the output to files and other targets, etc.

With that said, let’s jump in the code and give it a first try:

import logging

logging.debug('DEBUG')
logging.info('INFO')
logging.warning('WARNING')
logging.error('ERROR')
logging.critical('CRITICAL')

Upon running this, we should only see three outputs: WARNING / ERROR / CRITICAL.

This is because the default log level is set to warning, i.e. everything below warning is not printed.

To change that, we add a single line after the import:

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('DEBUG')
logging.info('INFO')
logging.warning('WARNING')
logging.error('ERROR')
logging.critical('CRITICAL')

… and now see all the output.

As teased in the beginning, there is a multitude of ways to customise logging behaviour. However, I’ll be brief and end this post here, as in my professional experience already these steps help in developing a scalable software architecture. Hope you enjoyed!

This post is part of a series show-casing important Python concepts quickly. You can find the other parts here:

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Python
Python Programming
Python3
Recommended from ReadMedium
avatarAbhay Kumar
OOPs in Python

An easy guide

10 min read