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
Edit: I tried it with maxBytes=1000000, but the result remained the same. The logfile just keeps getting bigger
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With