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!
source share