By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Logging module, a few questions

P: n/a
Hello
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?

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

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?
Thanks!

--Irmen de Jong
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
Sorry to reply to myself, but I found the solution.
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?
Never mind. I didn't read the manual very well. It tells me
to add a "datefmt" to the formatter, which works like a charm.
However, the other thing still stands:
I'm using a configuration file to set up the logging.

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?


Anybody got a good idea about this? Thanks!
--Irmen.
Jul 18 '05 #3

P: n/a
Irmen de Jong <irmen@-nospam-remove-this-xs4all.nl> wrote in message news:<40***********************@news.xs4all.nl>...
I'm using a configuration file to set up the logging.

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?


Since you are using a configuration file, you can set up config
entries for each logger for which you want to configure a level. For
example:

[loggers]
keys=root,log02,log03,log04

[logger_log02]
level=DEBUG
propagate=1
qualname=nl.company

[logger_log03]
level=WARN
propagate=1
qualname=nl.company.lib

[logger_log04]
level=WARN
propagate=1
qualname=org.apache

Regards,
Vinay Sajip
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.