【问题标题】:Python v3 LoggingPython v3 日志记录
【发布时间】:2011-03-04 06:31:37
【问题描述】:

我可以在 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 --> http://docs.python.org/library/logging.html#timedrotatingfilehandler

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

【问题讨论】:

    标签: python logging python-3.x


    【解决方案1】:

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

    据说它已在 Python 3.2 中修复。

    【讨论】:

    • 啊……说得有道理。非常感谢。
    • 3.1.4 也将包含这个错误修复,应该会在未来几个月的某个时间发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多