Pythonv3日志记录

时间:2011-03-04 06:31:37

标签: python logging python-3.x

我可以在Python 2上使用下面的代码。我在Python 3上尝试了它,但是我收到了一个错误。任何人都可以解释原因吗?

代码:

import logging
import logging.handlers

LOG_FILENAME = 'poller.log'

# Set up a specific logger with our desired output level
poll_logger = logging.getLogger('pollerLog')

# Add the log message handler to the logger
log_rotator = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when='d', interval=1, backupCount=5, encoding=None, delay=False, utc=False)
poll_logger.addHandler(log_rotator)

# Roll over on application start
poll_logger.handlers[0].doRollover()

错误:

Traceback (most recent call last):
  File "logR.py", line 10, in <module>
    log_rotator = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when='d', interval=1, back
upCount=5, encoding=None, delay=False, utc=False)
  File "C:\LynxApps\Python31\lib\logging\handlers.py", line 206, in __init__
    t = os.stat(filename)[ST_MTIME]
NameError: global name 'ST_MTIME' is not defined

我检查了下面的文件,我认为没有任何区别:

Python v2 - &gt; http://docs.python.org/library/logging.html#timedrotatingfilehandler

Python v3 - &gt; http://docs.python.org/py3k/library/logging.handlers.html?highlight=logging#timedrotatingfilehandler

1 个答案:

答案 0 :(得分:5)

这是Python 3.1.3中的一个错误(参见issue on bugs.python.org)。

据说它已在Python 3.2中修复。