470,870 Members | 1,416 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

mocking a logging object

In my unittest I want to override the logger of a working module so
that it puts all logging messages in /tmp/test.log instead so that in
my unittest I can inspect that it logs things correctly. Hopefully
this "pseudo" code will explain my problem::
>>import logging, os
logging.basicConfig(filename='/tmp/real.log', level=logging.INFO)
logger = logging.getLogger('Real')
logger.info('Real stuff')
os.path.isfile('/tmp/real.log')
True
>># do the monkey patching like the unit test does
logging.basicConfig(filename='/tmp/test.log', level=logging.INFO)
logger = logging.getLogger('Test')
logger.info('Test stuff')
os.path.isfile('/tmp/test.log')
False
>>open('/tmp/real.log').read()
'INFO:Real:Real stuff\nINFO:Test:Test stuff\n'

How can I change what file the logger should write to?
Jun 27 '08 #1
3 3093
Peter Bengtsson wrote:
In my unittest I want to override the logger of a working module so
that it puts all logging messages in /tmp/test.log instead so that in
my unittest I can inspect that it logs things correctly. Hopefully
this "pseudo" code will explain my problem::
>>>import logging, os
logging.basicConfig(filename='/tmp/real.log', level=logging.INFO)
logger = logging.getLogger('Real')
logger.info('Real stuff')
os.path.isfile('/tmp/real.log')
True
>>># do the monkey patching like the unit test does
logging.basicConfig(filename='/tmp/test.log', level=logging.INFO)
logger = logging.getLogger('Test')
logger.info('Test stuff')
os.path.isfile('/tmp/test.log')
False
>>>open('/tmp/real.log').read()
'INFO:Real:Real stuff\nINFO:Test:Test stuff\n'

How can I change what file the logger should write to?
You should simply attach a new handler to the logger in question that logs
the data into a stream/StringIO-object for later retrieval. Then remove
that handler after the test.

Diez
Jun 27 '08 #2
On Jun 2, 12:34*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Peter Bengtsson wrote:
In my unittest I want to override the logger of a working module so
that it puts all logging messages in /tmp/test.log instead so that in
my unittest I can inspect that it logs things correctly. Hopefully
this "pseudo" code will explain my problem::
>>import logging, os
logging.basicConfig(filename='/tmp/real.log', level=logging.INFO)
logger = logging.getLogger('Real')
logger.info('Real stuff')
os.path.isfile('/tmp/real.log')
True
>># do the monkey patching like the unit test does
logging.basicConfig(filename='/tmp/test.log', level=logging.INFO)
logger = logging.getLogger('Test')
logger.info('Test stuff')
os.path.isfile('/tmp/test.log')
False
>>open('/tmp/real.log').read()
'INFO:Real:Real stuff\nINFO:Test:Test stuff\n'
How can I change what file the logger should write to?

You should simply attach a new handler to the logger in question that logs
the data into a stream/StringIO-object for later retrieval. Then remove
that handler after the test.

Diez
Thanks! I'll try that.
Jun 27 '08 #3
On Jun 2, 12:34*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Peter Bengtsson wrote:
In my unittest I want to override the logger of a working module so
that it puts all logging messages in /tmp/test.log instead so that in
my unittest I can inspect that it logs things correctly. Hopefully
this "pseudo" code will explain my problem::
>>import logging, os
logging.basicConfig(filename='/tmp/real.log', level=logging.INFO)
logger = logging.getLogger('Real')
logger.info('Real stuff')
os.path.isfile('/tmp/real.log')
True
>># do the monkey patching like the unit test does
logging.basicConfig(filename='/tmp/test.log', level=logging.INFO)
logger = logging.getLogger('Test')
logger.info('Test stuff')
os.path.isfile('/tmp/test.log')
False
>>open('/tmp/real.log').read()
'INFO:Real:Real stuff\nINFO:Test:Test stuff\n'
How can I change what file the logger should write to?

You should simply attach a new handler to the logger in question that logs
the data into a stream/StringIO-object for later retrieval. Then remove
that handler after the test.

Diez
Yes! That worked. Thank you.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Eric DeWall | last post: by
6 posts views Thread by Ville Vainio | last post: by
1 post views Thread by flupke | last post: by
16 posts views Thread by Einar HÝst | last post: by
3 posts views Thread by Chris Smith | last post: by
2 posts views Thread by ZHENG Zhong | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.