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