logging module and doctest

G

Gary Jefferson

I've written a logging.filter and would like to use doctest on it
(using a StreamHandler for stdout), but this doesn't seem possible.
Output from the logger seems to disappear (running the doctest strings
through the interpreter as-is yields expected results). I assume this
is because doctest does something with logging. Is there any way to
make these work together?

Gary
 
P

Peter Otten

Gary said:
I've written a logging.filter and would like to use doctest on it
(using a StreamHandler for stdout), but this doesn't seem possible.
Output from the logger seems to disappear (running the doctest strings
through the interpreter as-is yields expected results). I assume this
is because doctest does something with logging.

It redirects stdout to a StringIO subclass to capture the output.
Is there any way to make these work together?

Using the StreamHandler with something like

class WrapStdOut(object):
def __getattr__(self, name):
return getattr(sys.stdout, name)

instead of sys.stdout directly should work.

Peter
 
P

Peter Otten

Peter said:
It redirects stdout to a StringIO subclass to capture the output.


Using the StreamHandler with something like

class WrapStdOut(object):
def __getattr__(self, name):
return getattr(sys.stdout, name)

instead of sys.stdout directly should work.

Or create the StreamHandler inside your doctest where sys.stdout is already
redirected.

Peter
 
G

Gary Jefferson

Peter said:
Or create the StreamHandler inside your doctest where sys.stdout is already
redirected.

Peter

I was creating the StreamHandler inside doctest. Turns out, however,
that I hadn't imported 'sys', and so it didn't work. Its a little
strange that this didn't result in an error, but there you have it.

Thanks for the tip that led me to discover this.

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top