Twisted provides a simple and flexible logging system in the twisted.python.log module. It has three commonly used functions:
Logs a new message. For example:from twisted.python import log log.msg('Hello, world.')
Writes a failure to the log, including traceback information (if any). You can pass it a Failure or Exception instance, or nothing. If you pass something else, it will be converted to a string with
If you pass nothing, it will construct a Failure from the currently active exception, which makes it convenient to use in an
exceptclause:try: x = 1 / 0 except: log.err() # will log the ZeroDivisionError
Starts logging to a given file-like object. For example:log.startLogging(open('/var/log/foo.log', 'w'))
or:from twisted.python.logfile import DailyLogFile log.startLogging(DailyLogFile.fromFullPath("/var/log/foo.log"))
startLoggingwill also redirect anything written to
sys.stderrto the log. You can disable this by passing
startLogging is called, log messages will be
discarded and errors will be written to stderr.
If you are using
twistd to run your daemon, it
will take care of calling
startLogging for you, and will also
rotate log files. See twistd and tac
twistd man page for details of using
If your application uses the Python standard library logging module or you want to use its easy configuration but don’t want to lose twisted-produced messages, the observer PythonLoggingObserver should be useful to you.
You just start it like any other observer:
observer = log.PythonLoggingObserver() observer.start()
Then configure the standard library logging module to behave as you want.
This method allows you to customize the log level received by the
standard library logging module using the
log.msg("This is important!", logLevel=logging.CRITICAL) log.msg("Don't mind", logLevel=logging.DEBUG)
logLevel is provided, logging.INFO is used for
logging.ERROR is used for
One special care should be made when you use special configuration of
the standard library logging module: some handlers (e.g. SMTP, HTTP) use the network and
so can block inside the reactor loop. Nothing in
done to prevent that.
Log observers are the basis of the Twisted logging system.
log.err ) is called, an
event is emitted. The event is passed to each observer which has been
registered. There can be any number of observers, and each can treat
the event in any way desired.
An example of
a log observer in Twisted is the
emit method of FileLogObserver .
FileLogObserver , used by
startLogging , writes events to a log file. A log observer
is just a callable that accepts a dictionary as its only argument. You can
then register it to receive all log events (in addition to any other
The dictionary will have at least two items:
The message (a list, usually of strings) for this log event, as passed to
log.msgor the message in the failure passed to
This is a boolean that will be true if this event came from a call to
log.err. If this is set, there may be a
failureitem in the dictionary as will, with a Failure object in it.
Other items the built in logging functionality may add include:
This message was captured from
sys.stdout, i.e. this message came from a
isErroris also true, it came from
You can pass additional items to the event dictionary by passing keyword
log.err . The standard
log observers will ignore dictionary items they don’t use.