All Articles

Python Logging Cheatsheet

Sometimes it’s nice to have a better logging than just print(). This post is just a cheatsheet of how to use python’s built in logging, which will hopefully save me from googling it every time.

My typical setup would look as follows. I put this in a that I can then easily import as import log. It sets up logs to go to stdout and a file, with the default level of DEBUG. Originally from StackOverflow somewhere, unfortunately I did not save the link.

import logging
import sys

file_handler = logging.FileHandler(filename='debug.log')
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]

	format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',

Import this somewhere in your entrypoint script. The format of your logs will look like this:

[2021-08-26 19:53:47,055] {} DEBUG - imgLoad 0.002850055694580078 s
[2021-08-26 19:53:47,064] {} DEBUG - removeJunk 0.008959770202636719 s
[2021-08-26 19:53:47,070] {} DEBUG - alignCharacters 0.004749298095703125 s
[2021-08-26 19:53:47,071] {} DEBUG - img resize 0.0004088878631591797 s
[2021-08-26 19:53:47,109] {} DEBUG - pytesseract 0.03712105751037598 s
[2021-08-26 19:53:47,109] {} DEBUG - doOcr 0.05709958076477051 s
[2021-08-26 19:53:47,109] {} INFO - Got challenge 246684713619

In your modules where you actually want to log things, you can set up a logger like so:

import logging
logger = logging.getLogger(__name__)"hello world")

Individual handlers can be set to different levels. This is useful if you want e.g. less verbose logs on stdout.

from log import stdout_handler

Further improvements

  • JSON formatted logs
  • Other log formats for easy lookup

Published Aug 29, 2021

Security Engineer with a dash of software. Originally from Stockholm, now in Berlin. I like to hack things.