3

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?

msg
  • 315
  • 2
  • 3
  • 12

1 Answers1

5

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.

msg
  • 315
  • 2
  • 3
  • 12
  • 1
    This is an old post but I think what you were actually after is a normal file handler https://devdocs.io/python~2.7/library/logging.handlers#logging.FileHandler Rotating file handlers are designed to make backups of old logs and rename them log.1, log.2 etc, see the docs in the same link for details – Hansang Jun 27 '19 at 01:34