3

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.

no0by5
  • 632
  • 3
  • 8
  • 30
  • AFAIK rotating should happen when the logging process creates the handler before midnight and makes a logging call destined for that handler after midnight. –  Jul 03 '17 at 10:28
  • Yes, that's what it does. But it deletes the old log and in the backup is only the first logging message after midnight. – no0by5 Jul 03 '17 at 11:51

0 Answers0