Twisted Log Level Switch

Is there a way in Twisted how to change the logging level of messages that should be logged?

I use three levels in the 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 set up logging 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 to establish which messages should be logged (for example, only information and error messages, lack of debugging). How to do it?

+4
source share
2 answers

Firstly, the api you are using does not exist. It is not documented in the module but log.msg documented here : all parameters without keywords are log.msg before log.msg are part of the message, therefore you do not install the message here but add an integer to your message, and this form is not recommended by the way.

 log.msg('agent nr.1 has free slots', logging.DEBUG) # debug message 

Secondly, to answer your question, yes, you could instruct the log level to determine which messages should be logged, but this is not how the default logger works. Fortunately, personalizing twisted is somewhat natural (if you know how to do this).

You need to write a log observer, for example, you can extend twisted.python.log.FileLogObserver , which processes 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 should 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 the --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 that 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') 
+10
source

I studied the answer from mg. and other sources and implemented a tiny tx-logging library. You will be able to record magazines in the general Putin way:

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

Visit the homepage for information: https://github.com/oblalex/tx-logging . Hope this helps someone in the future.

0
source

All Articles