Search code examples
pythonloggingtwisted

Python Twisted - How to set log levels for twisted.logger


Twisted recently came out with a new logging module: twisted.logger. I've read through the documentation[1], but I haven't been able to find where one sets the actual log level. The section on log observers[2] suggests that you might configure a predicate to do this if you are explicitly configuring your own LogObservers, but I am not sure how I am supposed to set the log level if I am running my app using twistd using either the --logfile or --syslog options.

Am I missing something in the documentation? Can someone provide a short example how I would use twistd and set the log level within my application to say, INFO?

[1] howto/logger

[2] howto/logger#provided-log-observers


Solution

  • Emitting at various log levels is done via the various emitter methods (log.debug, log.warn, etc.)

    The new module was added in a way meant to keep existing functionality exactly the same as before, so the log observers in use by twistd as the same as they were before and are not (yet) set up to do filtering based on log levels.

    Building that sort of functionality into twistd, as well as other goodies such as logging serialized JSON data instead of text (which can then be parsed and examined by software) are still forthcoming.

    In the meantime, you can set up your own log observers that do this, but that does require writing some code. You can write an observer that inspects the log_level key on inbound events, or use a FilteringLogObserver which will do that for you and forward the filtered events to your observer. The docs you referenced above should help you get started with that, if you want to use this now.

    There's a lot of potential here, such as being able to modify which log levels as passed along for specific modules at runtime, which is probably out of scope for twistd command-line options, but could be altered by a custom (eg. web) interface, or having only certain types of events go to a text log file and others sent via email, etc.

    Basically, we've been focusing on not breaking existing clients of Twisted, but do expect more and better integration with this new functionality over time.