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 log.py 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]

logging.basicConfig(
	level=logging.DEBUG,
	format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
	handlers=handlers
)

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

[2021-08-26 19:53:47,055] {ocr.py:89} DEBUG - imgLoad 0.002850055694580078 s
[2021-08-26 19:53:47,064] {ocr.py:95} DEBUG - removeJunk 0.008959770202636719 s
[2021-08-26 19:53:47,070] {ocr.py:107} DEBUG - alignCharacters 0.004749298095703125 s
[2021-08-26 19:53:47,071] {ocr.py:118} DEBUG - img resize 0.0004088878631591797 s
[2021-08-26 19:53:47,109] {ocr.py:134} DEBUG - pytesseract 0.03712105751037598 s
[2021-08-26 19:53:47,109] {pwn.py:41} DEBUG - doOcr 0.05709958076477051 s
[2021-08-26 19:53:47,109] {pwn.py:50} 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__)
logger.info("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
stdout_handler.setLevel(logging.INFO)

Further improvements

  • JSON formatted logs
  • Other log formats for easy lookup