[英]rename a log file in python using a logging module
I need to rename a logging file but continue logging to it afterwards.我需要重命名一个日志文件,但之后继续登录它。 I'm running Python 3.x and using a
logging
module to create logs.我正在运行 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')
Does anyone know if something like this is possible?有谁知道这样的事情是否可能? If no, how would I go about doing it?
如果没有,我将如何去做? One idea that I had was to close the file (
handler.close()
), remove the handler from the logger, rename the old file, create a new handler with the new file name and add it to the logger.我的一个想法是关闭文件(
handler.close()
),从记录器中删除处理程序,重命名旧文件,使用新文件名创建一个新处理程序并将其添加到记录器中。 The issue however is that I would need to create a completely identical handler, ie it would need to have the same attributes as the old one (with exception of the file name of course).然而,问题是我需要创建一个完全相同的处理程序,即它需要具有与旧处理程序相同的属性(当然文件名除外)。 Is there a simple way to store old attributes and then use them to set the attributes of the new handler?
有没有一种简单的方法来存储旧属性,然后使用它们来设置新处理程序的属性?
Any help is appreciated.任何帮助表示赞赏。
Late the party, but I ran into this problem as well and here's how I've (tentatively) solved it.晚会晚了,但我也遇到了这个问题,这是我(暂时)解决它的方法。 Needs more testing though.
虽然需要更多的测试。
Note in my case, I do have access to the previous handler, so I just wound up using the same handler configuration I used earlier.请注意,在我的情况下,我确实可以访问前一个处理程序,所以我只是使用了我之前使用的相同处理程序配置。 I agree a FileHandler.'rename_file()' method would be great.
我同意 FileHandler.'rename_file()' 方法会很棒。 One could likely be cooked up by sub-classing FileHandler(), but that's an exercise for another day.
一个可以通过子类化 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')
I had the same problem and was successful with little adaption of the solution suggested by JS我遇到了同样的问题,并且在几乎没有适应 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')
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)
The result should be 6 separate files, each with part of the log history for the application:结果应该是 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.