Logging in Python

“Down with print statements!”  We should all stop using them in our programs to “troubleshoot”.  Arguably if you can’t troubleshoot your program with a log message you are probably doing it wrong.  That said, I’m STILL not even close to meeting this ideal.  That said, I’m going to do my best to remedy this lack in my programming skillset.

Note:  This post will decidedly be less detailed than previous posts.  Basically this post is more of a reminder to myself on what is needed to add logging to my Python applications/programs.

Import the logging module

No surprise but you need to have the command import logging in the import section of your Python program.  No need to pip install logging because that module comes with the standard Python library.

import logging

It should be noted that you need to import logging in all your Python files in order to use logging in the Python code in that file.

Prep options for the logger object

The logging module has MANY variants and options.  This post only shows the barest minimum.  So, that said, to create a logger you need to set up some options (such as where the log messages should be outputted/stored and what is the format of the logging message).  With the following command I’m setting up the options to log to the stdout (aka the console), using a format that specifies the time, log level, and message and filters the log messages to only output a level of “DEBUG” or higher.

logging.basicConfig(format=’%(asctime)s %(levelname)s:%(message)s’, level=logging.DEBUG)

Note:  You only need to instantiate these options in your main program.  If you call/reference this line in other Python files they will be ignored.

Create the logger object

Now it is time to create the logging object.  In my case I called it log.

log = logging.getLogger(__name__)

In addition to assigning the logger object to a variable name you can generate a “name” for this object.  This “name” can be used in the format of the logging output to help you know what module/file is generating the log message you are looking at.

Note:  This command needs to be in ALL your Python files in which you want to log.

Create a log message

Now that we have a logger object we can “send” messages to it.  Since this basic logger (log) is set up to output to the stdout then this means it will behave very similar to how the print statement works.  Here is an example log message from one of my programs.

log.info(f”Opened and loaded {USERDATA_FILE}”)

When this line is executed it generates output like this:

2018-05-15 13:08:15,386 INFO:Opened and loaded userdata.yml

Notice that the format aligns with the format specified by the format option in the logging.basicConfig() line.

In this example I used “info” as the logging level.  Though you can create custom levels it is frowned upon.  Here is a list of logging level options: https://docs.python.org/3/library/logging.html#logging-levels