Reputation: 2663
I can't seem to log to stdout and run trial on the same bit piece of code.
temp.py
from sys import stdout
from twisted.python.log import startLogging
startLogging(stdout)
class Foo(object):
pass
temp_test.py
from twisted.trial import unittest
from temp import Foo
class FooTestCase(unittest.TestCase):
pass
output
2015-07-27 17:45:06-0400 [-] Log opened.
2015-07-27 17:45:06-0400 [-] Unable to format event {'log_namespace': 'twisted.logger._global', 'log_level': <LogLevel=warn>, 'fileNow': '/Users/james/Dropbox/code/demo/server/venv/lib/python2.7/site-packages/twisted/python/log.py', 'format': '%(log_legacy)s', 'lineNow': 210, 'fileThen': '/Users/james/Dropbox/code/demo/server/venv/lib/python2.7/site-packages/twisted/python/log.py', 'log_source': None, 'system': '-', 'lineThen': 210, 'log_logger': <Logger 'twisted.logger._global'>, 'time': 1438033506.184351, 'log_format': 'Warning: primary log target selected twice at <{fileNow}:{lineNow}> - previously selected at <{fileThen:logThen}>. Remove one of the calls to beginLoggingTo.', 'message': (), 'log_time': 1438033506.184351}: Invalid conversion specification
2015-07-27 17:45:06-0400 [-] Log opened.
Why is this code failing? It looks like it's trying to start logging twice?
I did notice that twisted.python.log
has been replaced by twisted.logger
; it should just be a wrapper around that. The Logger
class doesn't seem to have a direct analog to startLogging(stdout)
though.
Upvotes: 1
Views: 907
Reputation: 31860
Part of the problem here is a bug that has already been fixed in Twisted, with the formatting of the warning string here.
However, the real problem is that you are starting logging twice, once by running trial
, which sets up its own log observers for catching logged exceptions, and once by calling startLogging
at module scope. You'd get a similar error if you used the new API for doing this, so switching to twisted.logger
, while a good idea in the long run, isn't really related.
The proper solution is usually just to not start logging yourself; let twistd
or trial
do it for you. If you do need to start it yourself, it should be in the context of a main
function which is only run when starting your program "for real", outside of trial
, for example.
Upvotes: 1