Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit logfile size in Python logging

Tags:

python

logging

Limiting the log file size in python, as far as I can tell, can be done with the RotatingFileHandler and it's attribute maxBytes.

To test this, I wrote the following script:

import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)

# add a rotating handler
handler = RotatingFileHandler("test.log", maxBytes=1)
logger.addHandler(handler)

x = 0
while True:
    x += 1
    logger.info(x)

Even though the logfile should not exceed 1 byte, it goes up until many megabytes pretty quickly. How can I limit this? Sadly the documentation is pretty bad. I also wanted to know how to make sure that every time I rerun the python script, the log file is cleared and started fresh, however, in the docu it just says

class logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)

If mode is not specified, 'a' is used. 

However, it does not say what other options instead of 'a' there are.

To sum up the questions

  1. How can I limit the log file size to, let's say 10 megabytes
  2. How can I ensure a fresh logfile is started if the script is run, and not an old one appended

Edit: I tried it with maxBytes=1000000, but the result remained the same. The logfile just keeps getting bigger

like image 485
cheesus Avatar asked Aug 28 '26 12:08

cheesus


1 Answers

You can check the 2nd section of RotatingFileHandler class

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.

It looks like you didn't setup the backupCount value.

When backupCount is non-zero, the system will save old log files by appending the extensions ‘.1’, ‘.2’ etc., to the filename.

For example, with a backupCount of 5 and a base file name of app.log, you would get app.log, app.log.1, app.log.2, up to app.log.5. The file being written to is always app.log.

When this file is filled, it is closed and renamed to app.log.1, and if files app.log.1, app.log.2, etc. exist, then they are renamed to app.log.2, app.log.3 etc. respectively.

like image 156
Darkborderman Avatar answered Aug 31 '26 03:08

Darkborderman