How to prevent logging warning?
Thomas Heller
theller at python.net
Wed Oct 5 10:10:06 EDT 2005
> 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
More information about the Python-list
mailing list