1

I try to limit the size of log file in Python, using RotatingFileHandler.

I am using the following code:

logger = logging.getLogger(logging.basicConfig(
    filename = log_filename,
    handlers = [logging.handlers.RotatingFileHandler(filename=log_filename, maxBytes=1024, backupCount=20)],
    format = "%(asctime)s %(message)s",
    level = logging.DEBUG))

I visited here and here. I am using both maxBytes and backupCount parameters, but it is still not working. Any idea why the log file is not rotating?

Itai Klapholtz
  • 196
  • 1
  • 2
  • 15

1 Answers1

1

Python 2.7 does not support the handlers argument in logging.basicConfig() function. So you have to move the handler out of the basicConfig arguments, like that:

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(filename=log_filename, maxBytes=1024, backupCount=20)
handler.setFormatter("%(asctime)s %(message)s")
logger.addHandler(handler)