无法使用TimedRotatingFileHandler自定义时间戳格式来命名日志文件。

huangapple go评论67阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年8月10日 15:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定