TimedRotatingFileHandler doesn't create a correct backup after rotating

Viewed 343

I need a logger creating a new log file for every day, so I am using a TimedRotatingFileHandler and let it rotate at midnight. But every time it rotates, only the first logging message after midnight is stored in the backup file. The old log is deleted and the "main" log file is empty. That's how I create my logger:

def get_logger(name):   
    # Create the Logger
    logger = logging.getLogger(name)
    logger.setLevel(logging_lvl)

    # Create the Handler for logging data to a file
    logger_handler = TimedRotatingFileHandler(logging_filename, when='midnight', backupCount=7)
    logger_handler.setLevel(logging_lvl)

    # Create a Formatter for formatting the log messages
    logger_formatter = logging.Formatter(logging_format)

    # Add the Formatter to the Handler
    logger_handler.setFormatter(logger_formatter)

    # Add the Handler to the Logger
    logger.addHandler(logger_handler)

    all_logger[name] = logger

    return logger

Could the problem be, that I close my application just by pressing ctrl+c? Do I have to shutdown the FileHandler manually? I am using Python 3.4 on a Linux machine.

EDIT: logging_lvl, logging_filename, logging_lvl and logging_format are variables defined above.

0 Answers
Related