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 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
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 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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |