Logging is a very powerful way of validating your program executes correctly. In addition you can use it to debug your program.

If your program is logging, it means it keeps tracks of events that occur while your software is running. This means you can retrieve data from your program, even if it’s not an error or warning (level of severity).

You can log a process in your program using the module logging.

Related Course: Complete Python Programming Course & Exercises

Introduction

To use logging in Python, first load the logging module with the import command. The logging module is included with the Python Standard Library from version 2.3. If you use Python 3 or newer, it’s installed by default.

import logging

This message would be shown in the terminal.

 
import logging
logging.warning('Something went wrong.')

The reason you don’t want to use print() to log, is because it doesn’t include a severity level, timestamp, can’t log to a file etcetera.

Levels of severity

As you may know from programming, a message can have a certain severity. From Python programming you probably know there are errors and warnings. Where warnings you could ignore (but shouldn’t), errors stop your program.

The logging module comes with several levels of severity:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

Which type of logging can be displayed can be changed later in the program.

This means that while developing you could show everything (level=logging.DEBUG), but when the program is distributed to the customer you could show only error and criticial messages.

You can change the severity level with the function basicConfig(level=..).

logging.basicConfig(level=logging.WARNING)

Logging examples

The Python examples below show logging messages with different types of severity.
You can configure a minimum level of severity, if it’s lower than the set level it’s ignored.

 
import logging

logging.basicConfig(level=logging.WARNING)
logging.debug('Debug message')
logging.error('This is an error')

This outputs the error only, because the logging level is logging.WARNING. That means that debug messages are not shown (it’s higher in the list)

➜  ~ python example.py
ERROR:root:This is an error

If you want to show the debug message, change it to level=logging.DEBUG.

Another example:

 
import logging

logging.basicConfig(level=logging.ERROR)
logging.debug('Debug message')
logging.info('Program started..')
logging.info('Loading files')
logging.error('This is an error')

While developing you could set the severity level to logging.DEBUG which will show all messages. This will help you develop faster, find bugs quicker and so on.

Upon release of your program to the market you could set it to debugging.WARNING or debugging.ERROR.

import logging
logging.basicConfig(level=logging.WARNING)

Related Course: Complete Python Programming Course & Exercises

Logging to a file

You can write your log directly into a file. You can do that by adding filename= to the basicConfig() function call.

The severity level can be included, that means that if the severity is lower than the configured level, those messages won’t be written into the newly created file.

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message into the log file')
logging.info('Hello world should be in the file too')
logging.warning('Add some warning')

If you run the program and then output the file contents of your log, you’ll see messages that the Python programmed logged:

 ➜  ~ cat example.log 
DEBUG:root:This message into the log file
INFO:root:Hello world should be in the file too
WARNING:root:Add some warning

Logging variable data

You can log variables. The variables will then be included in your log messages (or file).
There are two ways to do this, one way is by using formatted strings (f-strings).

f-Strings start with an f before the quote, and can include variables in curly brackets.
This is very easy to read, but it requires a newer version of Python.

>>> x = 3
>>> logging.warning(f'the value of x is {x}')
WARNING:root:the value of x is 3
>>>

You can also do it the old fashioned way:

>>> logging.warning('the value os x is %d', x)
WARNING:root:the value os x is 3

Display date time in log

So far the log messages did not included time or data. You can easily include time and date into your log messages. To show time, you can define the format parameter.

import logging

logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
logging.debug('This is a debug log message')
logging.info('This is an informational log message')
logging.warning('Add some warning')

If you run the program you’ll see date and time included.

➜  ~ python3 example.py
2020-02-17 23:01:28.759 DEBUG example - <module>: This is a debug log message
2020-02-17 23:01:28.759 INFO example - <module>: This is an informational log message
2020-02-17 23:01:28.760 WARNING example - <module>: Add some warning

Related Course: Complete Python Programming Course & Exercises