logging Module#

Offical Documentation for the Logging Module

The logging module is one of the most valuable core modules built into the Python programming language. Like other Python core modules, to use it, simply add this line to the top of your program:

import logging

Levels of Logging#

There are five levels of logging available by default using the logging module. In order of severity, these are:

#. Debug - called with :p:logging.debug("Debugging message") #. Info - called with :p:logging.info("Informational message") #. Warning - called with :p:logging.warning("Warning message") #. Error - called with :p:logging.error("Error message") #. Critical - called with :p:logging.critical("Critical message")

Basic Usage#

After importing the module, it will require some basic setup. The most basic setup would be:

logging.basicConfig(level=logging.ERROR, filename="errors.log", filemode="a")

It is also possible to designate the format of the log string as follows:

logging.basicConfig(
    level=logging.ERROR, 
    filename="errors.log", 
    filemode="a",
    format="%(asctime)s - %(levelname)s - %(message)s"
)

The attributes that can be formatted and called in a LogRecord can be found here.

Embedding Variables in Logs#

It is very easy to write a custom string to a log using the logging module. Using a standard fstring, the value is written to the log. For example (continuing from the above examples):

myName = "Brad"
logging.error(f"Oh noes!  {myName} created an error!")

Embedding Tracebacks in Logs#

In Python, specific errors can be caught using a try/except block. The logging module has a special function built in to append caught errors to the log. For example:

try:
    1 / 0
except ZeroDivisionError:
    logging.exception("ZeroDivisionError")

Resources#

Tech with Tim - Python Logging

Socratica - Logging Module