Skip to content Skip to sidebar Skip to footer

Why Does Python Rotatingfilehandler's "maxbytes " Not Work

I'm trying to do a test run of the logging module's RotatingFileHandler as follows: from logging import getLogger, Formatter from logging.handlers import RotatingFileHandler MAX_L

Solution 1:

You need to read the documentation more carefully: you are missing the kwargs maxBytes and backupCount.

Replace this

handler = RotatingFileHandler(file_name, 'a+', MAX_LOG_SIZE)

for this

handler = RotatingFileHandler(file_name, 'a+', maxBytes=MAX_LOG_SIZE, backupCount=5)

Notice you shall set a backupCount value that fits your needs, I just used a random one.

A further explanation of why your piece of code does not roll the file is because the value backupCount is 0. See the following:

You can use the maxBytes and backupCount values to allow the file to rollover at a predetermined size. When the size is about to be exceeded, the file is closed and a new file is silently opened for output. 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.

Post a Comment for "Why Does Python Rotatingfilehandler's "maxbytes " Not Work"