Python Friday #62: Logging in Python

Logging allows us to understand what is going on inside our applications. Luckily for us, Python comes with a developer-friendly logger that works without much set-up code.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

The built-in logger

Python offers us a built-in logger, that is already pre-configured. This means we can directly write log messages without worrying about the set-up. All we need to do is to import logging and write the log messages with the level we need:

When we run our application, the default logger writes to the console, but only the log level warning, error and critical:

WARNING:root:A log message in level warning
ERROR:root:A log message in level error
CRITICAL:root:A log message in level critical

The downside of the built-in logger is that comes with settings that may not be what we want.

 

Configure the logger

We have many options to configure the logger to our needs. A first useful change may be to log all levels by setting the log level to DEBUG:

When we run our script again, it will print us all log messages:

DEBUG:root:A log message in level debug
INFO:root:A log message in level info
WARNING:root:A log message in level warning
ERROR:root:A log message in level error
CRITICAL:root:A log message in level critical

The more options we want to change, the better it is to do that in multiple steps and not just by pressing all the configuration into the logging.basicConfig() method. If we want to log to a file and change the format of the messages we can use this example configuration from Sjobeek:

If we run our script again, we get no output. The log messages are in the file test.log:

2021-02-22 18:46:05,383 [DEBUG] – A log message in level debug
2021-02-22 18:46:05,384 [INFO] – A log message in level info
2021-02-22 18:46:05,384 [WARNING] – A log message in level warning
2021-02-22 18:46:05,384 [ERROR] – A log message in level error
2021-02-22 18:46:05,384 [CRITICAL] – A log message in level critical

The ‘w’ in the file handler stands for write and works the same way as with files. With this option the log file is overwritten with every run of your script. If you want to append your log messages, you need the ‘a’ option (a = append):

 

Exceptions

The most important part of an exception is the stack trace. The logger offers us the special method logging.exception() to capture all we need to later figure out what went wrong (and where):

The log message for this exception looks like this (with the settings from above):

2021-02-22 18:46:55,755 [ERROR] – We got an exception
Traceback (most recent call last):
File “.\exceptionLogger.py”, line 17, in
result = 2 / 0
ZeroDivisionError: division by zero

 

Next

This covers the basics of logging. Next week we look at Flask and how we can log in our web applications.

4 thoughts on “Python Friday #62: Logging in Python”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.