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?
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With