本文介绍了将TimedRotatingFileHandler日志记录与logging.config一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用带有logging.config文件的TimedRotatingFileHandler进行测试,没有什么复杂的,但是应该每10秒滚动到一个新的日志文件中.
I'm trying to test using a TimedRotatingFileHandler with a logging.config file, nothing that complicated but it should roll over every 10 seconds into a new log file.
但是我得到以下内容
Traceback (most recent call last):
File "testLogging.py", line 6, in <module>
logging.config.fileConfig(logDir+'logging.conf')
File "C:\Python26\Lib\logging\config.py", line 84, in fileConfig
handlers = _install_handlers(cp, formatters)
File "C:\Python26\Lib\logging\config.py", line 159, in _install_handlers
h = klass(*args)
File "C:\Python26\Lib\logging\handlers.py", line 195, in __init__
raise ValueError("You must specify a day for weekly rollover from 0 to 6 (0
is Monday): %s" % self.when)
ValueError: You must specify a day for weekly rollover from 0 to 6 (0 is Monday)
: WHEN='S'
python非常简单,它只是设置了一个无限循环并不断记录日志
The python is pretty simple it just sets up an infinite loop that continually logs
import logging
import logging.config
logDir = "./logs/"
logging.config.fileConfig(logDir+'logging.conf')
logger = logging.getLogger('root')
while 1==1:
logger.info('THIS IS AN INFO MESSAGE')
和配置文件
[loggers]
keys=root
[logger_root]
level=INFO
handlers=timedRotatingFileHandler
[formatters]
keys=timedRotatingFormatter
[formatter_timedRotatingFormatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
datefmt=%m-%d %H:%M
[handlers]
keys=timedRotatingFileHandler
[handler_timedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=timedRotatingFormatter
args=('./logs/log.out', 'when=\'S\'', 'interval=10', 'backupCount=5')
正如您看到的那样,错误消息似乎表明了何时将其设置为"S"而不是"W".
As you can see when is set to 'S' not 'W' like the error message seems to indicate.
根据下面的答案,日志配置中的正确语法是
As per the answer below the correct syntax in the logging config was
args=('./logs/log.out', 'S', 10, 5, None, False, False)
推荐答案
args=('./logs/log.out', 'when=\'S\'', 'interval=10', 'backupCount=5')
看起来不正确.试试这个
Doesn't look right. Try this
args=('./logs/log.out', when='S', interval=10, backupCount=5)
或者可能是
args=('./logs/log.out','S',10,5)
这篇关于将TimedRotatingFileHandler日志记录与logging.config一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!