Python TimedRotatingFileHandler不';旋转后,无法创建正确的备份
我需要一个记录器为每天创建一个新的日志文件,所以我使用TimedRotatingFileHandler,让它在午夜旋转。但每次它旋转时,只有午夜后的第一条日志消息存储在备份文件中。旧日志被删除,“主”日志文件为空。这就是我创建记录器的方式:Python TimedRotatingFileHandler不';旋转后,无法创建正确的备份,python,logging,python-3.4,filehandler,Python,Logging,Python 3.4,Filehandler,我需要一个记录器为每天创建一个新的日志文件,所以我使用TimedRotatingFileHandler,让它在午夜旋转。但每次它旋转时,只有午夜后的第一条日志消息存储在备份文件中。旧日志被删除,“主”日志文件为空。这就是我创建记录器的方式: def get_logger(name): # Create the Logger logger = logging.getLogger(name) logger.setLevel(logging_lvl) # Cre
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
问题是,我只需按ctrl+c就可以关闭我的应用程序吗?我必须手动关闭FileHandler吗?
我正在Linux机器上使用Python 3.4
编辑:logging\u lvl、logging\u filename、logging\u lvl和logging\u format是上面定义的变量。AFAIK旋转应该发生在日志进程在午夜之前创建处理程序并在午夜之后为该处理程序进行日志调用时。是的,这就是它所做的。但它会删除旧日志,备份中只有午夜后的第一条日志消息。