How to prevent logging warning?
Neil Benn
benn at cenix-bioscience.com
Wed Oct 5 11:25:16 EDT 2005
Thomas Heller wrote:
>>Thomas Heller wrote:
>>
>>
>>>I'm about to add some logging calls to a library I have. How can I
>>>prevent that the script that uses the library prints
>>>'No handlers could be found for logger "comtypes.client"' when the
>>>script runs?
>>>I would like to setup the logging so that there is no logging when
>>>nothing is configured, and no warning messages are printed.
>>>
>>>
>
>Maksim Kasimov <maksim.kasimov at gmail.com> writes:
>
>
>
>>may be this you will find usefull:
>>
>>def getLog(logName, fileName = None):
>>
>> if fileName:
>> hdl = logging.FileHandler(fileName)
>> else:
>> hdl = logging.StreamHandler()
>>
>> fmt = logging.Formatter("%(name)s:\t%(levelname)s:\t%(asctime)s:\t%(message)s")
>> hdl.setFormatter(fmt)
>> log = logging.getLogger(logName)
>> log.addHandler(hdl)
>>
>> return log
>>
>>
>
>Not really - I know how to set up handlers, but I think it should be
>optional. Assume I have
>
> log = logging.getLogger("comtypes.client")
>
>and later
>
> log.warn("foo bar")
>
>in the library code.
>
>If I use the library an my script, I get the warning that I mentioned
>above. I want the script by default to be agnostic about the libraries
>logging. When I want to see the log messages, I can always do
>
> logging.basicConfig()
>
>in the script to see the log messages.
>
>I get the behaviour that I want when I add a 'NULL' handler in the
>library, but is this really how logging is intended to be used?
>
><library>
>log = logging.getLogger("comtypes.client")
>
>class NULLHandler(logging.Handler):
> def emit(self, *args):
> pass
>
>log.addHandler(NULLHandler())
></library>
>
>Thomas
>
>
Hello,
Absolutley not, I have exactly the same problem and it is
really annoying. The logging handlers should be set from an external
config file and not explicitly in the code. I simply had to just live
with it, it's annoying but there is nothing you can do about it (short
of playing around with stdout but avoiding that is why you used logging
in the first place!).
Cheers,
Neil
--
Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 46
D-01307
Dresden
Germany
Tel : +49 (0)351 4173 154
e-mail : benn at cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com
More information about the Python-list
mailing list