1

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())
  • python.org **Logging to a single file from multiple processes** Although logging is thread-safe, and logging to a single file from multiple threads in a single process is supported, logging to a single file from multiple processes is not supported, because there is no standard way to serialize access to a single file across multiple processes in Python. If you need to log to a single file from multiple processes, one way of doing this is to have all the processes log to a SocketHandler, and have a separate process which implements a socket server which reads from the socket and logs to file. – Abhishek Kumar Nov 17 '17 at 06:23
  • (If you prefer, you can dedicate one thread in one of the existing processes to perform this function.) This section documents this approach in more detail and includes a working socket receiver which can be used as a starting point for you to adapt in your own applications. If you are using a recent version of Python which includes the multiprocessing module, you could write your own handler which uses the Lock class from this module to serialize access to the file from your processes. – Abhishek Kumar Nov 17 '17 at 06:26

0 Answers0