Multiple log files using logging module

Luuk luuk at invalid.lan
Sun Mar 24 14:58:49 EDT 2019


On 24-3-2019 19:33, Peter Otten wrote:
> Luuk wrote:
> 
>> On 24-3-2019 18:13, Sharan Basappa wrote:
>>> I have a test program that imports a design program.
>>> Both the programs need to log messages.
>>>
>>> I have tried the following:
>>>
>>> 1) Both the programs have the following lines:
>>> for handler in logging.root.handlers[:]:
>>>       logging.root.removeHandler(handler)
>>>       
>>> #Create and configure logger
>>> filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
>>> '<>') logging.basicConfig(filename=filename, filemode='w',
>>> format='%(asctime)s %(message)s')
>>> #Creating an object
>>> logger = logging.getLogger()
>>> #Setting the threshold of logger to DEBUG
>>> logger.setLevel(logging.DEBUG)
>>>
>>> replace <> above with respective log file names for test and design
>>> programs. However, the test program logs the messages but not the design
>>> program.
>>>
>>> 2) I removed the above lines from design program altogether hoping that
>>> the messages will appear in the same log file. There is no error,
>>> however, no message is logged from the design program.
>>>
>>> I would like to get comment from members here as well as some simple
>>> programs to illustrate this ...
>>>
>>
>> As mentioned in your other thread, you should take note on HOW you put
>> the filenames in there.
> 
> I don't think that's the problem. Rather, if you call logging.basicConfig()
> multiple times in the same program only the first invocation has an effect,
> and only if there weren't any handlers added to the root logger by other
> means.
> 

It's not clear, at least not to me, if the programs are called from each 
other.

'test' and 'design' is not giving much info about what a program should do.

>>
>> How do you see the end of the line starting with 'filename =...'?
>>
>>
>> Is it like:
>> for TEST:    ...., 'TEST')
>> for DESIGN: ...., 'DESIGN')
>>
>> or did you put a full pathname in there?
>>
>> and, if you did put a full pathname in there (i.e. 'D:\TEMP\TEST' ),
>> did you also put the 'r' in front of it, like this:
>>      ...., r'D:\TEMP\TEST')
>>
>>
> 
> 


-- 
Luuk



More information about the Python-list mailing list