rename a log file in python using a logging module
Question: I need to rename a logging file but continue logging to it afterwards. I'm
running Python 3.x and using a `logging` module to create logs.

    
    
    logger = logging.getLogger(r'mylogger')
    handler = logging.FileHandler(r'mylog.txt', mode = r'w')
    logger.addHandler(handler)
    logger.info(r'msg 1')
    logger.info(r'msg 2')
    
    handler.renameFile(r'my_newlog.txt')  # <--- Is something like this possible?
    
    logger.info(r'msg 3')
    

Does anyone know if something like this is possible? If no, how would I go
about doing it? One idea that I had was to close the file (`handler.close()`),
remove the handler from the logger, rename the old file, create a new handler
with the new file name and add it to the logger. The issue however is that I
would need to create a completely identical handler, i.e it would need to have
the same attributes as the old one (with exception of the file name of
course). Is there a simple way to store old attributes and then use them to
set the attributes of the new handler?

Any help is appreciated.


Answer: 
    import glob
    import logging
    import logging.handlers
    
    LOG_FILENAME = 'logging_rotatingfile_example.out'
    
    # Set up a specific logger with our desired output level
    my_logger = logging.getLogger('MyLogger')
    my_logger.setLevel(logging.DEBUG)
    
    # Add the log message handler to the logger
    handler = logging.handlers.RotatingFileHandler(
                  LOG_FILENAME, maxBytes=20, backupCount=5)
    
    my_logger.addHandler(handler)
    
    # Log some messages
    for i in range(20):
        my_logger.debug('i = %d' % i)
    
    # See what files are created
    logfiles = glob.glob('%s*' % LOG_FILENAME)
    
    for filename in logfiles:
        print(filename)
    

The result should be 6 separate files, each with part of the log history for
the application:

    
    
    logging_rotatingfile_example.out
    logging_rotatingfile_example.out.1
    logging_rotatingfile_example.out.2
    logging_rotatingfile_example.out.3
    logging_rotatingfile_example.out.4
    logging_rotatingfile_example.out.5
    

