Logging module, a few questions

I

Irmen de Jong

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
 
P

Peter Otten

Irmen said:
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.

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?

But does it apply to the whole branch?

Peter
 
I

Irmen de Jong

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.
 
V

Vinay Sajip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top