RotatingFileHandler does not continue logging after error encountered
Question: 
    Traceback (most recent call last):
    File "/usr/lib64/python2.6/logging/handlers.py", line 76, in emit
        if self.shouldRollover(record):
    File "/usr/lib64/python2.6/logging/handlers.py", line 150, in       shouldRollover
         self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
    ValueError: I/O operation on closed file
    

I have a line in my script:

    
    
    handler = logging.handlers.RotatingFileHandler(cfg_obj.log_file,maxBytes = maxlog_size, backupCount = 10)
    

It runs fine when there are no error messages. But when there's an error log,
the logs after the error are not written to the file unless the process is
restarted. We do not want to restart the process every time there is an error.
Thanks for your help in advance!


Answer: I highly recommend you to use a configuration file. The configuration code
below "logging.conf" has different handlers and formatters just as example:

    
    
    [loggers]
    keys=root
    
    [handlers]
    keys=consoleHandler, rotatingFileHandler
    
    [formatters]
    keys=simpleFormatter, extendedFormatter
    
    [logger_root]
    level=DEBUG
    handlers=consoleHandler, rotatingFileHandler
    
    [handler_consoleHandler]
    class=StreamHandler
    level=DEBUG
    formatter=simpleFormatter
    args=(sys.stdout,)
    
    [handler_rotatingFileHandler]
    class=handlers.RotatingFileHandler
    level=INFO
    formatter=extendedFormatter
    args=('path/logs_file.log', 'a', 2000000, 1000)
    
    
    [formatter_simpleFormatter]
    format=%(asctime)s - %(levelname)s - %(message)s
    datefmt=
    
    [formatter_extendedFormatter]
    format= %(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(funcName)s() %(message)s
    datefmt=
    

Now how to use it "main.py":

    
    
    import logging.config
    
    # LOGGER
    logging.config.fileConfig('path_to_conf_file/logging.conf')
    LOGGER = logging.getLogger('root')
    try:
        LOGGER.debug("Debug message...")
        LOGGER.info("Info message...")   
    except Exception as e:    
        LOGGER.exception(e)
    

Let me know if you need more help.

