How to configure Python 2.3 protocol message format?

In Python 2.4 and later, setting up the logging module for simpler formatting is simple:

logging.basicConfig (level = opts.LOGLEVEL, format = "% (message) s")

but for applications that should support Python 2.3, this seems more complicated since the logging API has been redesigned in Py2.4. In particular, basicConfig does not accept any arguments. Having tried the option with the only example in the Py2.3 documentation, I get the following:

try: logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s") except: logging.getLogger().setLevel(opts.LOGLEVEL) h = logging.StreamHandler() h.setFormatter(logging.Formatter("%(message)s")) logging.getLogger().addHandler(h) 

but calling this root logger in Py2.3 for example

 logging.info("Foo") 

gives duplicate output:

 Foo INFO:root:Foo 

I can’t find a way to change the format of an existing handler in the root log in Py2.3 (the "except" block above), therefore calling addHandler, which creates duplicate output. Is there a way to set the root registrar format without this duplication? Thanks!

+4
source share
1 answer

except: the exception class [es] is a good way to get into trouble. I believe that the logging module in Python 2.3 has a basicConfig() function, but with fewer options. Since it accepts **kwargs , it can fail at any time after doing a certain job. I think that he already installed the handler with the default format, and then could not configure anything. After throwing an exception, you installed another handler. Having 2 handlers, you get 2 messages for each event. The easiest way in your case: do not use basicConfig() and configure logging manually. And never use except: unless you reraise or throw an exception.

+4
source

All Articles