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?