英文:
Unable to name log file with custom timestamp format using TimedRotatingFileHandler
问题
能否使用TimedRotatingFileHandler
创建具有自定义日期格式的日志文件?
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = 'logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + '_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1
生成的日志文件是:
20230810-100547_.log.2023-08-10_10-05-47
20230810-100547_.log.2023-08-10_10-05-48
20230810-100547_.log
而不是:
20230810-100547_.log
20230810-100548_.log
20230810-100549_.log
是否需要在午夜后重置处理程序(是否可能)?看起来不太优雅。
日志处理程序中似乎有一个namer属性。
英文:
Is it possible to create log files with custom date format, using TimedRotatingFileHandler
?
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = 'logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1
Produces
20230810-100547_.log.2023-08-10_10-05-47
20230810-100547_.log.2023-08-10_10-05-48
20230810-100547_.log
Instead of
20230810-100547_.log
20230810-100548_.log
20230810-100549_.log
Does one need to reset the handler after midnight (is it even possible)? Does not seem elegant.
There seem to be a namer attribute in logging handlers
答案1
得分: 1
有一个名为rotation_filename
的方法,你可以将其分配给namer
函数。它将在日志文件轮换期间更改日志文件的名称。工作代码应如下所示:
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
def namer(self):
now = datetime.now()
log_dir = './logs'
return os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + '_.log')
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = './logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + '_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.rotation_filename = namer
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1
如果需要进一步解释或有其他问题,请告诉我。
英文:
There is rotation_filename
method and you can assign it to namer function. It will change name of your log file during rotation. Working code should look like this:
import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime
import time
def namer(self):
now = datetime.now()
log_dir = './logs'
return os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
log_dir = './logs'
log_file = os.path.join(log_dir, datetime.now().strftime('%Y%m%d-%H%M%S') + f'_.log')
handler = TimedRotatingFileHandler(log_file, when='s', interval=1, backupCount=0)
handler.rotation_filename = namer
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
i = 0
while True:
logger.info(i)
print(i)
time.sleep(1)
i += 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论