簡體   English   中英

使用帶有logging.config的TimedRotatingFileHandler日志記錄

[英]Using TimedRotatingFileHandler logging with a logging.config

我正在嘗試使用帶有logging.config文件的TimedRotatingFileHandler進行測試,沒有任何復雜但它應該每隔10秒滾動到一個新的日志文件中。

但是我得到以下內容

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非常簡單,只是設置了一個不斷記錄的無限循環

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',就像錯誤消息似乎表明的那樣。

編輯:根據下面的答案,日志配置中的正確語法是

args=('./logs/log.out', 'S', 10, 5, None, False, False)
args=('./logs/log.out', 'when=\'S\'', 'interval=10', 'backupCount=5')

看起來不對。 試試這個

args=('./logs/log.out', when='S', interval=10, backupCount=5)

或者可能這樣

args=('./logs/log.out','S',10,5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM