473,387 Members | 1,569 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,387 software developers and data experts.

Logging with multiple loggers/handlers

I'm having some difficulty getting my logging configuration set
correctly. I'm using a config file (copied at end of post), with the
intent of setting several loggers which write to a combination of a
file, stderr (for debugging), and the NT Eventlog, but I don't seem to
be getting the right combination of handlers called.

The desired combinations are as follows:

root - Eventlog, stderr
log02 - file, stderr
log02.log03 - file, stderr
log02.log04 - Eventlog, file, stderr

Currently, logging to stderr is working for all four loggers, but only
log02 is being written to the logfile. Also, log03 is writing to the
Eventlog even though it shouldn't be reaching that handler at all.

Any suggestions as to how I could change my logconfig.ini to get the
desired effect? (I'm considering creating an 'init' logger off of the
root, which would handle the Eventlog records, thus allowing me to
propagate all the stderr records to root, but this doesn't seem like
it'd directly affect the parts I'm having problems with...)

For reference, I'm using Python 2.2.1 and v0.4.9.2 of the logging
package. (Upgrading to a newer version of Python is not preferred,
though it could be done if it'll resolve the problem, but I've gathered
that the logging package *should* work fine with Py2.2.)

Jeff Shannon
Technician/Programmer
Credit International

----- logconf.ini -------------------------
[loggers]
keys=root,log02,log03,log04

[handlers]
keys=hand01,hand02,hand03

[formatters]
keys=form01

[logger_root]
level=NOTSET
propagate=1
channel=
parent=
qualname=(root)
handlers=hand01,hand03

[logger_log02]
level=DEBUG
propagate=0
channel=log02
parent=(root)
qualname=log02
handlers=hand02,hand03

[logger_log03]
level=DEBUG
propagate=1
channel=log03
parent=log02
qualname=log02.log03
handlers=

[logger_log04]
level=DEBUG
propagate=1
channel=log04
parent=log02
qualname=log02.log04
handlers=hand01

[handler_hand01]
class=handlers.NTEventLogHandler
level=DEBUG
formatter=form01
appname=LogTest
dllname=
logtype=Application
args=('LogTest', '', 'Application')

[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
filename=Logs\python.log
mode=a
args=('Logs\python.log', 'a')

[handler_hand03]
class=StreamHandler
level=DEBUG
formatter=form01
stream=sys.stderr
args=(sys.stderr,)

[formatter_form01]
format=%(asctime)s %(name)s %(levelname)s %(message)s
datefmt=

Jul 18 '05 #1
3 5251
To follow up to my own post -- I've found my problem, and it appears to
have been between my ears. ;P

I somehow managed to misinterpret the spec for logging.getLogger().
From the current docs:

getLogger([name]):
Return a logger with the specified name or, if no name is specified,
return
a logger which is the root logger of the hierarchy.

All calls to this function with a given name return the same logger
instance.
This means that logger instances never need to be passed between
different
parts of an application.

Combined with this bit from PEP 282:
Loggers are never instantiated directly. Instead, a module-level
function is used:

def getLogger(name=None): ...

If no name is specified, the root logger is returned. Otherwise,
if a logger with that name exists, it is returned. If not, a new
logger is initialized and returned. Here, "name" is synonymous
with "channel name".

And the descriptions of the config file
(http://www.red-dove.com/python_logging.html#config):

#The channel value indicates the lowest portion of the channel name
of the
#logger. For a logger called "a.b.c", this value would be "c".

All of this combined to give me the impression that the name that's
passed to getLogger() should be the "lowest portion" channel name,
rather than the fully qualified name -- i.e., that I should be using
"log03" instead of "log02.log03". As it turns out, my impression was
wrong -- it's the fully qualified name that should be used. My attempts
to use the unqualified name were creating new, unconfigured loggers
(with no handlers attached) that could only propagate records up to the
root logger. I realized this only after inspecting the log manager's
loggerDict and seeing the unconfigured "extra" loggers.

While it's fairly likely that my interpretation of the docs is not
something that many people will come up with, I think it might be
worthwhile to expand the example sections to actually show how to use
multiple heirarchical loggers. Had there been an example to follow, I
certainly wouldn't have made this mistake.

Jeff Shannon
Technician/Programmer
Credit International
Jul 18 '05 #2
> Loggers are never instantiated directly. Instead, a module-level
function is used:

def getLogger(name=None): ...

If no name is specified, the root logger is returned. Otherwise,
if a logger with that name exists, it is returned. If not, a new
logger is initialized and returned. Here, "name" is synonymous
with "channel name".

And the descriptions of the config file
(http://www.red-dove.com/python_logging.html#config):

#The channel value indicates the lowest portion of the channel name
of the
#logger. For a logger called "a.b.c", this value would be "c".

All of this combined to give me the impression that the name that's
passed to getLogger() should be the "lowest portion" channel name,
rather than the fully qualified name -- i.e., that I should be using
"log03" instead of "log02.log03". As it turns out, my impression was
wrong -- it's the fully qualified name that should be used. While it's fairly likely that my interpretation of the docs is not
something that many people will come up with, I think it might be
worthwhile to expand the example sections to actually show how to use
multiple heirarchical loggers. Had there been an example to follow, I
certainly wouldn't have made this mistake.


The example in http://www.red-dove.com/python_logging.html#config does
show in bold the items used by the configuration API, and says that
the others are just used by the GUI configurator. Perhaps "channel"
was the wrong name to use for a private element in the configurator,
but it's done now.
Jul 18 '05 #3
Vinay Sajip wrote:
While it's fairly likely that my interpretation of the docs is not
something that many people will come up with, I think it might be
worthwhile to expand the example sections to actually show how to use
multiple heirarchical loggers. Had there been an example to follow, I
certainly wouldn't have made this mistake.


The example in http://www.red-dove.com/python_logging.html#config does
show in bold the items used by the configuration API, and says that
the others are just used by the GUI configurator. Perhaps "channel"
was the wrong name to use for a private element in the configurator,
but it's done now.


May I suggest changing the wording of the getLogger() docs? I think
that simply adding a few words to a single sentence will clarify things
considerably.

- Here, "name" is synonymous with "channel name".
+ Here, "name" is synonymous with "fully qualified channel name".

Jeff Shannon
Technician/Programmer
Credit International

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...
2
by: chuck | last post by:
I want to create two different loggers so that users see one output (e.g. no exception/stack traces) and others (e.g support staff) that does see stack traces via email. The code below is close...
1
by: Rene Pijlman | last post by:
I'd expect the program below to put only one line with "Eat me" in the log file. However, I get this with Python 2.4.2 in WingIDE on Windows XP: Eat me Ignore me My understanding is: 1....
10
by: Baurzhan Ismagulov | last post by:
Hello all, I want that each module has its own logger. I've defined the following config file: keys=f01 keys=console
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
1
by: Almad | last post by:
Hi, our applications can have plugins as subpackages and I'd like to allow them to use their own logger as well as it's configuration. I thought that best way will be their own configuration...
2
by: Robert | last post by:
Hi all I have been trying to set up a framework with logging implemented using the built in Python logging module. For better or worse, I have decided to base the logger names on the module...
3
by: Ross Boylan | last post by:
I would like my different threads to log without stepping on each other. Past advice on this list (that I've found) mostly says to send the messages to a Queue. That would work, but bypasses...
6
by: Larry Bates | last post by:
Every time I look at the logging module (up until now) I've given up and continue to use my home-grown logger that I've been using for years. I'm not giving up this time ;-) I find that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.