扭曲的原木液位开关 [英] twisted log level switch
问题描述
Twisted中有什么方法可以更改应该记录的消息的记录级别?
Is there any way in Twisted how to change logging level of messages which should be logged?
我在项目中使用了三个级别:
I am using three levels in project:
log.msg('agent nr.1 has free slots', logging.DEBUG) # debug message
log.msg('agent nr.1 has free slots') # info message
log.err('agent nr.1 has free slots') # error message
我以这种方式配置日志记录:
And I configure logging in this way:
from twisted.python import log
from twisted.python.logfile import LogFile
logfile = LogFile("someFile.log", '/some/path/', rotateLength=1000, maxRotatedFiles=100)
application.setComponent(log.ILogObserver, log.FileLogObserver(logfile).emit)
但是我需要设置应该记录哪些消息(例如,仅显示信息和错误消息,不进行调试).怎么做?
But I need set which messages should be logged (e.g. only info and error messages, no debug). How to do it?
推荐答案
首先,您使用的api不存在.它没有记录在模块上
级别,但log.msg
是此处记录的:传递了所有非关键字参数
到log.msg
是消息的一部分,因此在这里您不设置消息
级别,但在您的消息中添加一个整数,这种形式是顺便说一句
灰心丧气.
First, the api you are using does not exists. It is not documented at module
level but log.msg
is documented here: all non keywords parameters passed
to log.msg
are part of the message so here you are not setting a message
level but adding a integer to your message and this form is by the way
discouraged.
log.msg('agent nr.1 has free slots', logging.DEBUG) # debug message
第二,回答您的问题,是的,您可以指示twist使用 日志级别来确定应记录哪些消息,但这不是方法 默认记录器有效.幸运的是,个性化扭曲有点自然 (如果您知道该怎么做).
Second, to answer to your question, yes, you could instruct twisted to use a log level to determinate which messages should be logged but this is not how the default logger works. Fortunately, personalize twisted is somewhat natural (if you know how to do it).
您必须编写一个记录器观察器,例如,您可以扩展
twisted.python.log.FileLogObserver
,用于处理logLevel:
You have to write a logger observer, for example you can extend
twisted.python.log.FileLogObserver
, that handles a logLevel:
import logging
from twisted.python import log
class LevelFileLogObserver(log.FileLogObserver):
def __init__(self, f, level=logging.INFO):
log.FileLogObserver.__init__(self, f)
self.logLevel = level
def emit(self, eventDict):
if eventDict['isError']:
level = logging.ERROR
elif 'level' in eventDict:
level = eventDict['level']
else:
level = logging.INFO
if level >= self.logLevel:
log.FileLogObserver.emit(self, eventDict)
然后您必须注册它:
from twisted.python import logfile
f = logfile.LogFile("someFile.log", '/some/path/', rotateLength=1000,
maxRotatedFiles=100)
logger = LevelFileLogObserver(f, logging.DEBUG)
twisted.python.log.addObserver(logger.emit)
如果使用双绞线,则可以通过--logger
选项进行传递:
If you use twistd you can pass it through --logger
option:
# mylogger.py
# import LevelFileLogObserver
from twisted.python import logfile
f = logfile.LogFile("someFile.log", '/some/path/', rotateLength=1000,
maxRotatedFiles=100)
flobserver = LevelFileLogObserver(f)
observer = flobserver.emit
# twistd invocation
twistd --logger=mylogger.observer
现在您可以使用您定义的新api:
Now you can use the new api you defined:
log.msg('the level of this message is INFO')
log.msg('the level of this message is INFO', level=logging.INFO)
log.msg('the level of this message is DEBUG', level=logging.DEBUG)
log.msg('the level of this message is ERROR', level=logging.ERROR)
log.err('the level of this message is ERROR')
这篇关于扭曲的原木液位开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!