Log rotation - python and windows

(I searched and did not find a duplicate for this question, but I am happy that it will be proved otherwise).

I need to rotate the log from some Python code. The code runs on Windows (Server 2008 R2).

I originally used TimedRotatingFileHandler(from the Python package logging.handlers), but this does not work as we need it because of what I understand, it is a problem that it encounters multiprocessing (subprocess.check_call is used to run another attachment).

I checked ConcurrentLogHandler, which looks like it can do the job, but I'm a little worried that it has not been updated since 2013 (although problems have been raised quite recently).

UPDATE : open error (since 2013) indicates that ConcurrentLogHandler is not working with Python 2.7 / Windows. When registering, the code just freezes.

Is there a better windows solution I should use?

+4
source share
2 answers

Maybe I missed something, but the Python logging module comes with RotatingFileHandler:

import logging
import time

from logging.handlers import RotatingFileHandler

#----------------------------------------------------------------------
def create_rotating_log(path):
    """
    Creates a rotating log
    """
    logger = logging.getLogger("Rotating Log")
    logger.setLevel(logging.INFO)

    # add a rotating handler
    handler = RotatingFileHandler(path, maxBytes=20,
                                  backupCount=5)
    logger.addHandler(handler)

    for i in range(10):
        logger.info("This is test log line %s" % i)
        time.sleep(1.5)

#----------------------------------------------------------------------
if __name__ == "__main__":
    log_file = r"c:\path\to\test.log"
    create_rotating_log(log_file)

This worked for me with Python 2.7 on Windows 7. Here are some links that are described in more detail below:

0
source

OK is what I did.

( ), . - , (logging.shutdown - , doco , ...), . .

try/except, - , .

, , .

logging.shutdown - , ?!

0

All Articles