473,385 Members | 1,445 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,385 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 3233
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;...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.