python - 更改扭曲矩阵日志中的时间格式

标签 python logging twisted

如何更改 TwistedMatrix 中使用的日志系统的时间格式?

我注意到了 http://twistedmatrix.com/trac/browser/tags/releases/twisted-11.0.0/twisted/python/log.py#L389 应该允许更改时间格式,但它不起作用 对我来说,这是我执行的完整测试程序 python myscript.py

from twisted.internet import endpoints, reactor

from twisted.python import log
from twisted.application.service import Application
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile

#[... here my definition of a ProxyFactory()...]

application = Application("myapp")
logfile = DailyLogFile("my.log", './')
flo = FileLogObserver(logfile)
flo.timeFormat = "%Y-%m-%d %H:%M:%S,%f%z"
application.setComponent(ILogObserver, flo.emit)

log.startLogging(logfile)
log.msg("this is a test")

endpoint = endpoints.serverFromString(reactor, portstr)
d = endpoint.listen(ProxyFactory())
d.addErrback(shutdown, reactor)
reactor.run()

没有得到预期的:“%Y-%m-%d %H:%M:%S,%f%z”(毫秒)

2013-06-12 17:08:07+0200 [-] Log opened.
2013-06-12 17:08:12+0200 [-] this is a test

我错过了什么?

还有:

  • 当我不需要文件时,我该如何继续改变这个时间格式 记录但只打印 stderr?

(其他引用:http://twistedmatrix.com/trac/ticket/3513)

编辑:我试图重新表述我的两个问题。

所以从 JeanPaul 发布的答案中,我了解到我正在将东西和经典 python 文件与另一个 tac 文件混合(在阅读 JeanPaul 之前我不知道)。顺便说一句,我在下面尝试了这个,但仍然没有得到我需要的毫秒数:

(这次我启动twistd -noy my.tac)

from twisted.application.service import Application
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile

application = Application("myapp")
logfile = DailyLogFile("my.log", "./")
flo = FileLogObserver(logfile)
flo.timeFormat = "%Y-%m-%d %H:%M:%S,%f %z"
application.setComponent(ILogObserver, flo.emit)

并得到:

2013-06-13 17:23:23,%f+0000 [-] Log opened.
2013-06-13 17:23:23,%f+0000 [-] using set_wakeup_fd
2013-06-13 17:23:23,%f+0000 [-] twistd 12.0.0 (/usr/bin/python 2.7.3) starting up.
2013-06-13 17:23:23,%f+0000 [-] reactor class: twisted.internet.pollreactor.PollReactor.
2013-06-13 17:23:30,%f+0000 [-] Received SIGINT, shutting down.
2013-06-13 17:23:30,%f+0000 [-] Main loop terminated.
2013-06-13 17:23:30,%f+0000 [-] Server Shut Down.

如你所见,如果我模仿 @http://twistedmatrix.com/trac/browser/trunk/twisted/python/log.py#L351 所做的事情,看第367行,python和时间给我这个毫秒数。还要注意 %Z 是错误的,它应该是 +0200,但我可以忍受它,而我需要毫秒......

Python 2.7.3 (default, Jan  2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now().strftime("%H:%M:%S.%f")
'17:28:06.566135'
>>> import time
>>> when = time.time()
>>> import datetime
>>> datetime.datetime.fromtimestamp(when).strftime("%Y-%m-%d %H:%M:%S,%f%z")
'2013-06-13 17:33:20,535350'
>>> import twisted
>>> twisted.version
Version('twisted', 12, 0, 0)

最佳答案

您在这里对日志系统做了几件不同的事情。您要求 twistd 使用 flo.emit 作为日志观察器。这将在 twistd 加载此 tac 文件 中定义的应用程序 后生效。然后,您手动初始化日志系统以使用 logfile 并立即记录一条消息。由于这是 tac 文件的一部分,twistd 尚未完成加载 application,因此它尚未应用您指定的日志记录配置ILogObserver。相反,日志事件由您使用 startLogging - logfile 设置的日志观察器处理,它对自定义时间戳格式一无所知。

停止 startLogging 调用,您应该会看到 应用程序tac 加载后记录的事件> 文件格式正确。

您可以在 logging howto 中阅读所有关于日志记录在 Twisted 中如何工作的信息。以及如何在 application howto 中为 twistd 配置它.

另请注意,支持 custom time formatting using datetime.strftimefirst introduced in Twisted 13.0.0 .从您问题的输出来看,您似乎拥有 Twisted 12.0.0。这意味着格式化是使用 time.strftime 完成的,它不支持微秒。

在Twisted 13.0.0之前,要获取时间格式中的微秒,需要覆盖FileLogObserverformatTime方法,调用datetime.strftime 你自己。

关于python - 更改扭曲矩阵日志中的时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17071029/

相关文章:

python - django+uwsgi 使用 TimedRotatingFileHandler "overwrites rotated log file"进行日志记录

python - Scrapy 导入错误

python - 为什么 check_password_hash 在此登录 View 中始终返回 False?

python - 使用正则表达式在字符串中查找两个相同的字符

ruby - 在 Ruby 中将错误写入日志文件

java - 在大型 Java 应用程序中标准化日志记录的方法

python - 字典中的重复项(Python)

python - 从列表中删除包含日期的字符串,而不影响列表中的独立日期

python - 如何可靠地获取twistd生成进程的pid?

python - 扭曲的 Python 暂停/推迟 react 器