twisted log level switch

好久不见. 提交于 2019-12-07 11:19:20

问题


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?


回答1:


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

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).

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)

then you have to register it:

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)

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

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')



回答2:


I've studied the answer from mg. and other sources and implemented a tiny library tx-logging. You will be able to write logs in common pythonic way:

LOG = tx_logging.getLogger("some log name")
LOG.debug("some message")

Visit home page for info: https://github.com/oblalex/tx-logging. Hope this will help someone in future.



来源:https://stackoverflow.com/questions/13748222/twisted-log-level-switch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!