I have created a module having various scripts in the root as well as the sub-folders. I was previously creating a logging script which defined a central logger instance and I was using RotatedFileHandler for creating the log file. The problem is I am unable to find a condition which can decide that the execution of the module has ended and logger instance requires a doRollOver. What to do?

import os
import glob
import logging
import logging.handlers
import time

LOG_FILENAME = str(os.getcwd()+'/logs/log-.out')

# Set up a specific logger with our desired output level
logger = logging.getLogger(__name__)

needRoll = <CONDITION?>

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)

logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# This is a stale log, so roll it
if needRoll:
# Add timestamp
    logger.debug('---------Log closed on %s.---------' % time.asctime())

    # Roll over on application start
    logger.handlers[0].doRollover()

# Add timestamp
logger.debug('---------Log started on %s.---------' % time.asctime())
0

There are 0 best solutions below