How to prevent logging warning?
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. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
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 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. > > Thomas -- Best regards, Maksim Kasimov mailto: [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
> 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 <[EMAIL PROTECTED]> 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? log = logging.getLogger("comtypes.client") class NULLHandler(logging.Handler): def emit(self, *args): pass log.addHandler(NULLHandler()) Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
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 <[EMAIL PROTECTED]> 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? > > >log = logging.getLogger("comtypes.client") > >class NULLHandler(logging.Handler): >def emit(self, *args): >pass > >log.addHandler(NULLHandler()) > > >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 : [EMAIL PROTECTED] Cenix Website : http://www.cenix-bioscience.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
[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. This is probably a gross hack, but then I think one could argue that the one-time "No handlers could be found for" warning is a misfeature -- at least without a clean way to suppress it. I don't know the history of that warning though: -- mylib.py --- import logging log = logging.getLogger("mylib") def func(): log.warn("don't go near the river") log.error("I'm drowning!") -- -- myscript.py --- import sys import logging import mylib if __name__ == "__main__": if "-v" in sys.argv: logging.basicConfig() else: logging.Logger.manager.emittedNoHandlerWarning = True mylib.func() -- $ python myscript.py $ python myscript.py -v WARNING:mylib:don't go near the river ERROR:mylib:I'm drowning! hackily yours, Trent -- Trent Mick [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
Thomas Heller wrote: > 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? I would support ...err, what's the opposite of a feature request? Anyway, another hack, slightly more lightweight/intrusive: logging.root.manager.emittedNoHandlerWarning = True Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
Thomas Heller wrote: > 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? > The reason for the one-off message is that without it, a misconfiguration or a failure to configure any handlers is notified to a user (who is possibly not used to the logging package). I'm not sure which is more annoying - a one-off message which occurs when no handlers are configured and yet events are logged, or complete silence from logging when something is misconfigured, and not giving any feedback on what's wrong? (It's a rhetorical question - the answer is of course quite subjective). Certainly, I could change things so that e.g. the error is suppressed when logging.raiseExceptions is set to 0 (typically for production use). Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
s/without/with/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
"Vinay Sajip" <[EMAIL PROTECTED]> writes: > Thomas Heller wrote: > >> 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? >> > > The reason for the one-off message is that without it, a > misconfiguration or a failure to configure any handlers is notified to > a user (who is possibly not used to the logging package). I'm not sure > which is more annoying - a one-off message which occurs when no > handlers are configured and yet events are logged, or complete silence > from logging when something is misconfigured, and not giving any > feedback on what's wrong? (It's a rhetorical question - the answer is > of course quite subjective). I do *not* think 'no handler' is a misconfiguration. Is it possible to differentiate between a misconfiguration and 'no configuration'? > Certainly, I could change things so that e.g. the error is suppressed > when logging.raiseExceptions is set to 0 (typically for production > use). That would be fine. But there are also other ways - you could, for example, print the warning only when __debug__ is False. And you could use the warnings module instead of blindly printing to stderr, this way it could also be filtered out. BTW: Since I have your attention now, is the graphical utility to configure the logging.conf file still available somewhere, and compatible with the current logging package (with 'current' I mean the one included with Python 2.3.5)? Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
Thomas Heller wrote: > I do *not* think 'no handler' is a misconfiguration. Is it possible to > differentiate between a misconfiguration and 'no configuration'? It's a fair point. The line was more blurred when the logging package was newly released into the wild ;-) But "no configuration" could be caused e.g. by an unreadable config file, which might also be categorised as a "misconfiguration". > That would be fine. But there are also other ways - you could, for > example, print the warning only when __debug__ is False. And you could > use the warnings module instead of blindly printing to stderr, this way > it could also be filtered out. Compatibility with 1.5.2 precludes use of the warnings module. If using raiseExceptions meets your requirement, I'll use that. I'm not sure it's a good idea for the behaviour to change between running with and without -O. > BTW: Since I have your attention now, is the graphical utility to > configure the logging.conf file still available somewhere, and > compatible with the current logging package (with 'current' I mean > the one included with Python 2.3.5)? You can always get my attention via email :-) The graphical utility (logconf.py) is available from the download at http://www.red-dove.com/python_logging.html#download AFAIK it should work OK with 2.3.5, though I haven't tested it recently as there wasn't much interest in it. In fact you're the first person to ask! It generates a few extra entries in the config file which are used by the utility only, which are seemingly regarded as "cruft" by most people. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: How to prevent logging warning?
I have now checked a change into CVS whereby the one-off error message is not printed unless raiseExceptions is 1. The default behaviour is thus unchanged, but if you set raiseExceptions to 0 for production use and then don't configure any handlers, then the message is not printed. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list