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? 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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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)....
|
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...
|
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;...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |