Python Logging
Python has a built-in logging library which is powerful and flexible enough for most logging needs. This page is a basic guide to the built-in logging library.
You import the library with import logging
.
The Basics
The most basic usage is to just import the library and use the logging.info()
(and .debug()
, .warning()
, .error()
, .critical()
) functions for logging.
import logging
logging.info("This is an info message.")logging.debug("This is a debug message.")logging.warning("This is a warning message.")logging.error("This is an error message.")logging.critical("This is a critical message.")
By default, the logging library will log messages to the console (i.e. you ran python main.py
in the terminal, you would see this output).
Coloured Logging
The logging library does not include any coloured output by default. Coloured output is useful when printing to terminals as you can use colours like red for errors to quickly catch your attention. Most terminals support ANSI escape codes to change the text colours (among other things). Whilst you could output the escape codes yourself with the standard library, the colorlog
library makes this easier.
The below example shows how to set up the logging library to use the colorlog
library for coloured output. Install colorlog
with pip install colorlog
.
import loggingimport colorlog
# ==================================================================================================# Logging Setup# ==================================================================================================
handler = colorlog.StreamHandler()handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s'))
# Setup root logger, this will apply to all loggerslogger = logging.getLogger()logger.handlers.clear()logger.addHandler(handler)logger.setLevel(logging.DEBUG)
This gives the coloured output shown in This is a placeholder for the reference: fig-coloured-logs-example-in-python.