Reputation: 325
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?
Upvotes: 3
Views: 12236
Reputation: 1
As per python docs: "Rollover occurs whenever the current log file is nearly maxBytes in length; but if either of maxBytes or backupCount is zero, rollover never occurs, so you generally want to set backupCount to at least 1, and have a non-zero maxBytes." https://docs.python.org/3/library/logging.handlers.html
Upvotes: 0
Reputation: 325
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.
Upvotes: 5