473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Logging module, a few questions

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
3 2248
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
8
by: Steve Erickson | last post by:
I have a logger class that uses the Python logging module. When I call it within a program using the unittest module, I get one line in the log file for the first test, two identical ones for the...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
4
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.