Using doctrine and logging in a python program
#!/usr/bin/python2.4
import logging
import sys
import doctest
def foo(x):
"""
>>> foo (0)
0
"""
print ("%d" %(x))
_logger.debug("%d" %(x))
def _test():
doctest.testmod()
_logger = logging.getLogger()
_logger.setLevel(logging.DEBUG)
_formatter = logging.Formatter('%(message)s')
_handler = logging.StreamHandler(sys.stdout)
_handler.setFormatter(_formatter)
_logger.addHandler(_handler)
_test()
I would like to use the logger module for all my print statements. I reviewed the first 50 best Google links for this, and they seem to agree that doctest uses its own copy of stdout. If printing is used, it works; if a log is used, it is logged in the root console. Can someone please demonstrate a working example with a code snippet that will allow me to integrate. Note that the nose for doctrine testing simply adds the log output at the end of the test (assuming you set the radio buttons), it does not treat them as a print statement. A.
, , , DocTestRunner run:
#imports left out, code not tested - just to point you in the right direction
class MyDocTestRunner(DocTestRunner):
def run(self, test, compileflags=None, out=None, clear_globs=True):
if out is None:
handler = None
else:
handler = StreamHandler(out)
logger = logging.getLogger() # root logger (say)
if handler:
logger.addHandler(handler)
try:
DocTestRunner.run(self, test, compileflags, out, clear_globs)
finally:
if handler:
logger.removeHandler(handler)
handler.close()
DocTestRunner.
doctest, , - , addHandler(logging.Streamhandler(sys.stdout)) .
, , logger :
"""
This is a doctest that will capture output from the logging module.
>>> logger.addHandler(logging.StreamHandler(sys.stdout))
The rest of your doctest, now able to use output from the logging
module...
"""
: doctest , doctest , sys.stdout DocTestRunner._fakeout. logging.StreamHandler sys.stdout, sys.stdout sys.stdout, sys.stdout.
, doctest, _fakeout _SpoofOut, , .
Gotchas:
error: [Errno 128] Transport endpoint is not connected
, , import sys.