回答
我正在努力解决类似的问题。 The first result on Google for "twisted logs" is pretty helpful 该网页导致我Application page其中有定制的应用程序的记录行为的一个例子:
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", "/tmp")
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
我猜我可以做到这一点,并使用FileLogObserver的自定义子类。我去了,看着那个代码/usr/lib/python2.6/dist-packages/twisted/python/log.py
这里是
class FileLogObserver:
"""
Log observer that writes to a file-like object.
@type timeFormat: C{str} or C{NoneType}
@ivar timeFormat: If not C{None}, the format string passed to strftime().
"""
timeFormat = None
def __init__(self, f):
self.write = f.write
self.flush = f.flush
def getTimezoneOffset(self, when):
"""
Return the current local timezone offset from UTC.
@type when: C{int}
@param when: POSIX (ie, UTC) timestamp for which to find the offset.
@rtype: C{int}
@return: The number of seconds offset from UTC. West is positive,
east is negative.
"""
offset = datetime.utcfromtimestamp(when) - datetime.fromtimestamp(when)
return offset.days * (60 * 60 * 24) + offset.seconds
def formatTime(self, when):
"""
Format the given UTC value as a string representing that time in the
local timezone.
By default it's formatted as a ISO8601-like string (ISO8601 date and
ISO8601 time separated by a space). It can be customized using the
C{timeFormat} attribute, which will be used as input for the underlying
C{time.strftime} call.
@type when: C{int}
@param when: POSIX (ie, UTC) timestamp for which to find the offset.
@rtype: C{str}
"""
if self.timeFormat is not None:
return time.strftime(self.timeFormat, time.localtime(when))
tzOffset = -self.getTimezoneOffset(when)
when = datetime.utcfromtimestamp(when + tzOffset)
tzHour = abs(int(tzOffset/60/60))
tzMin = abs(int(tzOffset/60 % 60))
if tzOffset < 0:
tzSign = '-'
else:
tzSign = '+'
return '%d-%02d-%02d %02d:%02d:%02d%s%02d%02d' % (
when.year, when.month, when.day,
when.hour, when.minute, when.second,
tzSign, tzHour, tzMin)
def emit(self, eventDict):
text = textFromEventDict(eventDict)
if text is None:
return
timeStr = self.formatTime(eventDict['time'])
fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t")}
msgStr = _safeFormat("[%(system)s] %(text)s\n", fmtDict)
util.untilConcludes(self.write, timeStr + " " + msgStr)
util.untilConcludes(self.flush) # Hoorj!
def start(self):
"""
Start observing log events.
"""
addObserver(self.emit)
def stop(self):
"""
Stop observing log events.
"""
removeObserver(self.emit)
我知道这是不是一个解决方案,但这是我迄今为止所了解到的。如果我想出其他东西,我会发布它。
这里是我重写发射功能:
from twisted.python import log, util
from twisted.internet import reactor
from twisted.application.service import Application
def myFLOemit(self,eventDict):
"""Custom emit for FileLogObserver"""
text = log.textFromEventDict(eventDict)
if text is None:
return
self.timeFormat='[%Y-%m-%d %H:%M:%S]'
timeStr = self.formatTime(eventDict['time'])
fmtDict = {'text': text.replace("\n", "\n\t")}
msgStr = log._safeFormat("%(text)s\n", fmtDict)
util.untilConcludes(self.write, timeStr + " " + msgStr)
util.untilConcludes(self.flush)
# Overwrite twistd's FileLogObserver formatting
log.FileLogObserver.emit=myFLOemit
# Start the app
application=Application("fileget")
reactor.callWhenRunning(log.msg,"No system here!")
和所得到的输出:
$ twistd -noy myapp.tac
[2012-02-06 12:32:22] Log opened.
[2012-02-06 12:32:22] twistd 11.1.0 (/usr/bin/python2 2.7.2) starting up.
[2012-02-06 12:32:22] reactor class: twisted.internet.pollreactor.PollReactor.
[2012-02-06 12:32:22] No system here!
猴子修补全球功能(这是一种方法,但它仍然是全球性的 - 每个人都分享它):不是一个好主意。 Nathan的答案好多了。 – 2012-02-06 22:36:09
我可以像这样在应用程序上使用setComponent:http://snipt.org/zppK0但是twistd的-l完全被忽略。你有建议适用于标准输出和日志文件吗? – 2012-02-07 18:55:14
我想通过检查如何调用twistd来使用自定义观察者:http://snipt.org/uIL0 – 2012-02-10 19:16:36
使用@ Nathan的方向一个完整的例子:
- 1. 扭曲的日志记录
- 2. 在扭曲的矩阵日志中更改时间格式
- 3. 扭曲:禁用扭曲框架类的日志记录
- 4. 扭曲XML格式
- 5. 扭曲的日志级别开关
- 6. 与django扭曲的日志记录
- 7. 扭曲的插件日志记录
- 8. 使用Python日志记录与扭曲
- 9. 如何在filebeat中格式化日志
- 10. Python扭曲 - 如何设置twisted.logger的日志级别
- 11. 扭曲:如何沉默某些日志消息?
- 12. 日志行格式化
- 13. 将日志程序信息以扭曲形式存入文件
- 14. 扭曲格式与轨道脚手架
- 15. 好的MSbuild日志格式化程序?
- 16. 如何打开扭曲的某些文件的新日志文件
- 17. 扭曲。如何为每个请求的日志写一个唯一的前缀
- 18. 扭曲和扭曲Movieclip
- 19. 读取日志文件的最后几行,扭曲
- 20. 扭曲:如何隐式传递参数?
- 21. 错误的格式化日志消息;日志记录已写入日志
- 22. httplib2扭曲的方式
- 23. 模式在扭曲的Python
- 24. SolrJetty日志记录 - 如何让自定义日志格式化程序工作?
- 25. Jetty6 slf4j日志记录 - 如何让自定义日志格式化工作?
- 26. 如何更改Tornado的日志格式
- 27. 如何配置Karma日志的格式?
- 28. 嵌套/格式化日志记录
- 29. 增强日志 - 使用severity_channel_logger格式化
- 30. 使用Spark/Scala格式化日志
谢谢,I”米也是这样。 – 2010-09-13 21:39:59