我需要重命名日志文件,但之后继续记录。我正在运行Python 3.x并使用logging
模块来创建日志。
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')
有人知道这样的事情是否可行?如果不是,我该怎么做呢?我的一个想法是关闭文件(handler.close()
),从记录器中删除处理程序,重命名旧文件,使用新文件名创建一个新处理程序并将其添加到记录器。然而问题是我需要创建一个完全相同的处理程序,即它需要具有与旧的相同的属性(当然文件名除外)。是否有一种简单的方法来存储旧属性,然后使用它们来设置新处理程序的属性?
感谢任何帮助。
答案 0 :(得分:1)
晚会,但我也遇到了这个问题,而且我在这里(试探性地)解决了这个问题。不过需要更多测试。
注意在我的情况下,我确实可以访问前一个处理程序,所以我最后使用了我之前使用的相同处理程序配置。我同意一个FileHandler。&#39; rename_file()&#39;方法会很棒。有人可能会通过对FileHandler()进行子类化来制作,但这是另一天的练习。
#!/usr/bin/env python3
import os
import logging
logger_name1 = 'my_log.txt'
logger_name2 = 'my_new_log.txt'
logger = logging.getLogger('mylogger')
logger.level = logging.INFO
handler = logging.FileHandler(logger_name1, mode='w')
logger.addHandler(handler)
logger.info('msg 1')
logger.info('msg 2')
# handler.renameFile('my_newlog.txt') # <--- Is something like this possible?
logger.removeHandler(handler)
# Rename the logfile on disk
os.rename(logger_name1, logger_name2)
# New handler using new filename. Note the 'append' flag
new_handler = logging.FileHandler(logger_name2, mode='a')
logger.addHandler(new_handler)
# Try out the new logfile
logger.info('msg 3')
答案 1 :(得分:0)
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)
结果应该是6个单独的文件,每个文件都包含应用程序的部分日志历史记录:
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
答案 2 :(得分:0)
我遇到了同样的问题,并且在不适应JS建议的解决方案的情况下获得了成功
#!/usr/bin/env python3
import os
import logging
import shutil
logger_name1 = 'my_log.txt'
logger_name2 = 'my_new_log.txt'
logger = logging.getLogger('mylogger')
logger.level = logging.INFO
handler = logging.FileHandler(logger_name1, mode='w')
logger.addHandler(handler)
logger.info('msg 1')
logger.info('msg 2')
# handler.renameFile('my_newlog.txt') # <--- Is something like this possible?
handler.close() # need to close to release file handle
logger.removeHandler(handler)
# Rename the logfile on disk
#os.rename(logger_name1, logger_name2) # error if destination already exists
shutil.move(logger_name1, logger_name2)
# New handler using new filename. Note the 'append' flag
new_handler = logging.FileHandler(logger_name2, mode='a')
logger.addHandler(new_handler)
# Try out the new logfile
logger.info('msg 3')