473,288 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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 3226
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric DeWall | last post by:
In trying to clean up the inevitable debug printing littered through some code, I started reading up on the 'logging' module. Browsing groups indicates that the design of the module is...
15
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values)...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
1
by: flupke | last post by:
Hi, i'm trying to log to the same file (using logging module) from different classes but i can't seem to get it to work. File 1 is the main file which sets up the logger and then i want to also...
16
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some...
3
by: Chris Smith | last post by:
Hola, pythonisas: The documentation for the logging module is good, but a bit obscure. In particular, there seems to be a lot of action at a distance. The fact that getLogger() can actually be a...
0
by: rshekhtm | last post by:
Hi everyone, I would like to get your opinion on a technique I came up with when faced with the problem of redundant code in every web method (authentication, logging, exception handling)....
6
by: Silfheed | last post by:
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for...
2
by: ZHENG Zhong | last post by:
Hi, I implemented a small logging library with the API like this: logger& log = log_manager::instance().get_logger("my_logger"); log.stream(DEBUG) << "this is a debug message" << std::endl;...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.