Python 3 logger with RotatingFileHandler excedes maxBytes limit

Viewed 8690

I am using the following code to limit the size of a logfile (minimal example):

import logging
from logging.handlers import RotatingFileHandler

# Set up logfile and message logging.
logger = logging.getLogger("Logger")
logger.setLevel(logging.ERROR)
# Create the rotating file handler. Limit the size to 1000000Bytes ~ 1MB .
handler = RotatingFileHandler("test.log", mode='a', maxBytes=1000000, encoding=None, delay=0)
handler.setLevel(logging.ERROR)
# Create a formatter.
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add handler and formatter.
handler.setFormatter(formatter)
logger.addHandler(handler)

for l in range(1000):
    logger.error("test" * 1000)

However the file size excedes the limit of 1MB and keeps logging. I don't see what I am doing wrong. Is there a way to choose different parameters to correctly limit the size of my logfile?

1 Answers

Ok it seems I found a workaround that solves my problem:

import logging
from logging.handlers import RotatingFileHandler

# Set up logfile and message logging.
logger = logging.getLogger("Logger")
logger.setLevel(logging.ERROR)
# Create the rotating file handler. Limit the size to 1000000Bytes ~ 1MB .
handler = RotatingFileHandler("test.log", mode='a', maxBytes=1000000, backupCount=1, encoding='utf-8', delay=0)
handler.setLevel(logging.ERROR)
# Create a formatter.
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add handler and formatter.
handler.setFormatter(formatter)
logger.addHandler(handler)

for l in range(1000):
    logger.error("test" * 1000)

I simply missed out on the backupCount=1 parameter. This however creates two files test.log and test.log.1 (2 files seem to be needed for correct file rotations). Not the perfect solution since I would like to only have one file but it works well for me.

Related