[Twisted-Python] Dumb Logger API question

Glyph Lefkowitz glyph at twistedmatrix.com
Tue Nov 10 19:05:19 MST 2015


> On Nov 9, 2015, at 2:29 PM, Kevin Mcintyre <kebin70 at gmail.com> wrote:
> 
> http://twistedmatrix.com/documents/current/core/howto/logger.html <http://twistedmatrix.com/documents/current/core/howto/logger.html>
> 
> I'm looking at the new-ish logger api and wonder how to log to console as a first step to transitioning.
> 
> Any pointers?  Thanks.

Do you mean "emit structured log text to stdout"?  In that case, that is covered in the document you link to; these two sections:

http://twistedmatrix.com/documents/current/core/howto/logger.html#avoid-mutable-event-keys
http://twistedmatrix.com/documents/current/core/howto/logger.html#starting-the-global-log-publisher

create a jsonFileLogObserver and start logging to it:

import sys
from twisted.logger import jsonFileLogObserver, globalLogBeginner, Logger
globalLogBeginner.beginLoggingTo([jsonFileLogObserver(sys.stdout)])
log = Logger()
log.info("Information.")

If you want unstructured log output for human reading (not a good choice for an automated system, but perhaps useful for debugging), you can instead go with textFileLogObserver, in almost exactly the same way:

import sys
from twisted.logger import textFileLogObserver, globalLogBeginner, Logger
globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)])
log = Logger()
log.info("Information.")

Is this what you were looking for?

-glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20151110/9ad25322/attachment-0002.html>


More information about the Twisted-Python mailing list