Discussion:
[Twisted-Python] Dumb Logger API question
Kevin Mcintyre
2015-11-09 22:29:45 UTC
Permalink
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.
Glyph Lefkowitz
2015-11-11 02:05:19 UTC
Permalink
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
Kevin Mcintyre
2015-11-11 05:09:22 UTC
Permalink
yes, I stared at that section for a bit. Not to get off topic, but the
readability of the api docs from back when is slightly better in my
opinion. So the topic being I'm an idiot - thanks for both
examples...that's a perfect example.

I got confused by "from myobservers import PrintingObserver" cause I'm
wondering if myobservers is a core package.
Post by Kevin Mcintyre
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
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
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
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
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Loading...