Logging module, a few questions

Peter Otten __peter__ at web.de
Sun May 9 05:15:38 EDT 2004


Irmen de Jong wrote:

> I'm using the logging module in Python 2.3.3, with a format string
> containing %(asctime). But it now dumps a full date +timestamp in
> the log, which is nice but sometimes I only want the timestamp
> (no date). Is there an easy way to change this? How can I make
> %(asctime) dump only the time?

>>> import logging as lg
>>> logger = lg.getLogger("sample")
>>> hd = lg.StreamHandler()
>>> fm = lg.Formatter("%(asctime)s %(message)s", "%H:%M:%S")

> I'm using a configuration file to set up the logging.

I think in a config file that would be

[formatter_xxx]
format=%(asctime)s %(message)s
datefmt=%H:%M:%S

> Which brings me to another thing.
> In java's log4j, it's easy to "silence" or "enable" specific
> parts of a logging hierarchy, by doing:
> 
> log4j.category.nl.company = DEBUG
> log4j.category.nl.company.lib = WARN
> log4j.category.org.apache = WARN
> 
> and so on.
> 
> I see no easy way of doing the same for Python's logging module;
> it seems that I have to create a handler for each of the different
> parts of the hierarchy of which I want to set the loglevel.
> Am I missing something?

>>> hd.setFormatter(fm)
>>> root = lg.getLogger()
>>> root.setLevel(lg.WARNING)
>>> root.addHandler(hd)
>>> logger.warn("so what")
10:15:59 so what
>>> logger.info("so what")
>>> logger.setLevel(lg.INFO)
>>> logger.info("so what")
10:16:50 so what
>>> root.info("so what")

But does it apply to the whole branch?

>>> sublogger = lg.getLogger("sample.sub")
>>> sublogger.info("so what")
10:26:11 so what
>>> logger.setLevel(lg.WARNING)
>>> sublogger.info("so what")
>>>

Peter




More information about the Python-list mailing list