18.7. Logging Rotation

18.7.1. BaseRotatingHandler

  • logging.handlers.BaseRotatingHandler

is the base class for handlers that rotate log files at a certain point. It is not meant to be instantiated directly. Instead, use RotatingFileHandler or TimedRotatingFileHandler.

18.7.2. RotatingFileHandler

  • logging.handlers.RotatingFileHandler

instances send messages to disk files, with support for maximum log file sizes and log file rotation.

18.7.3. TimedRotatingFileHandler

  • logging.handlers.TimedRotatingFileHandler

instances send messages to disk files, rotating the log file at certain timed intervals.

18.7.4. Example

from logging.handlers import TimedRotatingFileHandler
from logging import Formatter

handler = TimedRotatingFileHandler(filename='/tmp/myfile.log', when='midnight')
formatter = Formatter('%(asctime)s, %(levelname)s, %(message)s', datefmt='%Y-%m-%d, %H:%M:%S')
handler.setFormatter(formatter)