Luke Luke - 1 month ago 4x
Python Question

use doctest and logging in python program

import logging
import sys
import doctest
def foo(x):
>>> foo (0)
print ("%d" %(x))
_logger.debug("%d" %(x))
def _test():
_logger = logging.getLogger()
_formatter = logging.Formatter('%(message)s')
_handler = logging.StreamHandler(sys.stdout)

I would like to use logger module for all of my print statements. I have looked at the first 50 top google links for this, and they seem to agree that doctest uses it's own copy of the stdout.
If print is used it works if logger is used it logs to the root console.
Can someone please demonstrate a working example with a code snippet that will allow me to combine.
Note running nose to test doctest will just append the log output at the end of the test, (assuming you set the switches) it does not treat them as a print statement.


I'm not sure why you want to do this, but if you really need to do it, then you can define your own subclass of DocTestRunner, and override the run method:

#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
            handler = StreamHandler(out)
        logger = logging.getLogger() # root logger (say)
        if handler:
  , test, compileflags, out, clear_globs)
            if handler:

Then use this runner in place of DocTestRunner.