gpt4 book ai didi

Python v3 日志记录

转载 作者:行者123 更新时间:2023-11-28 22:05:31 25 4
gpt4 key购买 nike

我可以在 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 3.1.3 中的错误(请参阅 issue on bugs.python.org)。

据说它在 Python 3.2 中已修复。

关于Python v3 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5190470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com