Simple logging with rotation and custom format in Python

Sometimes I find myself having to write a short Python script with proper logging capabilities.

This code snippet demonstrates how to do use Python’s official logging module using very few lines but still with all these features in:

  • basicConfig
  • log rotation
  • formating
import logging
logging.basicConfig(
	handlers=[
		RotatingFileHandler(
			'/path/to/log/file.log',
			maxBytes=10240000,
			backupCount=5
		)
	],
	level=logging.INFO,
	format='%(asctime)s %(levelname)s PID_%(process)d %(message)s'
)
logging.info('Hello world')
logging.error('Oh no, an error occurred!')

The following lines are interesting:

  • ‘/path/to/log/file.log’ -> lets you specify where the log file is
  • maxBytes=10240000 -> maximum size of a log before being rotated
  • backupCount=5 -> how many rotated files to keep
  • format=’%(asctime)s %(levelname)s PID_%(process)d %(message)s’ -> format of each log line; pay attention to the PID, very useful if your script can be invoked simultaneously multiple times, because this will let you split logs from different parallel runs