Joshua Hedges
Joshua Hedges

Reputation: 317

Do you have to reinstantiate the logging logger to have it perform file cleanup?

I'm working with the code in question: How to use Python's RotatingFileHandler

Specifically this bit...

import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('my_log.log', maxBytes=2000, backupCount=10)
logger.addHandler(handler)

for _ in range(10000):
    logger.debug('Hello, world!')

I put this at the beginning of my code, but my requirements dictate that I have to build this into a daemon, so the code at the core of the application will run forever.

Do I have to reinstantiate the logger for log cleanup to occur, or is the check made every time a log is written?

Upvotes: 0

Views: 90

Answers (1)

Matheus Portela
Matheus Portela

Reputation: 2470

According to the official documentation, file management is done automatically by the logger when it is writing to the file, so there wouldn't be a need to re-instantiate the logger to trigger this behavior:

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.

This is further corroborated by the emit method source code. emit is called to actually log the message when its severity level is equal or higher than the one configured in your logger:

def emit(self, record):
    """
    Emit a record.
    Output the record to the file, catering for rollover as described
    in doRollover().
    """
    try:
        if self.shouldRollover(record):
            self.doRollover()
        logging.FileHandler.emit(self, record)
    except Exception:
        self.handleError(record)

As you can see, it tries to roll over the files whenever necessary before logging the message.

Upvotes: 1

Related Questions