classification
Title: [PATCH] logging.config.fileConfig() compulsivly disable all existing loggers
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: anacrolix, llucax, vinay.sajip
Priority: normal Keywords: patch

Created on 2008-06-19 01:02 by llucax, last changed 2011-04-08 10:53 by anacrolix. This issue is now closed.

Files
File name Uploaded Description Edit
logging.config.disable_existing_loggers.patch llucax, 2008-06-19 01:01 Add fileConfig() option to choose if you want to disable existing loggers (patch)
logging.config.disable_existing_loggers.doc.patch llucax, 2008-06-20 00:41 Add some documentation on the new fileConfig() argument (to be applied to r64416 or higher)
Messages (6)
msg68388 - (view) Author: Leandro Lucarella (llucax) Date: 2008-06-19 01:01
When using logging.config.fileConfig() in a large project, with several
nested loggers, is very counterintuitive and annoying that this function
disable all non-configured-via-fileConfig() loggers, because it forces
the config file to configure *all* the loggers there, which makes the
file unmaintainable, and throws the beauty of hierarchical loggers to
the trash.

Attached is a sample patch that adds a new option
"disable_existing_loggers" to fileConfig() function (defaulting to True,
to make it backward-compatible) to control this behavior. If you like
the idea I can update the documentation and add some testcases too.

You can see a simple example about the problem and solution here:
log.py:  http://pastebin.lugmen.org.ar/4204
log.ini: http://pastebin.lugmen.org.ar/4205

without the patch, the output is:
logger:DEBUG: log debug
logger:INFO: log info
logger:WARNING: log warning
logger:ERROR: log error
logger:CRITICAL: log critical

With the patch (and passing disable_existing_loggers=False to
fileConfig()) yields this output:
logger:DEBUG: log debug
logger:INFO: log info
logger:WARNING: log warning
logger:ERROR: log error
logger:CRITICAL: log critical
logger.sublogger:DEBUG: sublog debug
logger.sublogger:INFO: sublog info
logger.sublogger:WARNING: sublog warning
logger.sublogger:ERROR: sublog error
logger.sublogger:CRITICAL: sublog critical

As one could expect when reading the logging module docs:
"""
getLogger() returns a reference to a logger instance with the specified
if it it is provided, or root if not. The names are period-separated
hierarchical structures. Multiple calls to getLogger() with the same
name will return a reference to the same logger object. Loggers that are
further down in the hierarchical list are children of loggers higher up
in the list. For example, given a logger with a name of foo, loggers
with names of foo.bar, foo.bar.baz, and foo.bam are all children of foo.
Child loggers propagate messages up to their parent loggers. Because of
this, it is unnecessary to define and configure all the loggers an
application uses. It is sufficient to configure a top-level logger and
create child loggers as needed.
"""
msg68391 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-06-19 07:59
Without the patch, but simply moving the fileConfig() call to above
where the loggers are initialised, the output is the same as your
patched sample, viz.

logger:DEBUG: log debug
logger:INFO: log info
logger:WARNING: log warning
logger:ERROR: log error
logger:CRITICAL: log critical
logger.sublogger:DEBUG: sublog debug
logger.sublogger:INFO: sublog info
logger.sublogger:WARNING: sublog warning
logger.sublogger:ERROR: sublog error
logger.sublogger:CRITICAL: sublog critical

See

http://pastebin.lugmen.org.ar/4208
msg68404 - (view) Author: Leandro Lucarella (llucax) Date: 2008-06-19 13:02
The problem is you can't always do that call before using the loggers.

In my case, I get the logging config file via command-line arguments
(using optparse), but before I can't even know what logging config file
to load, I have log calls.

Please reopen the bug, the actual solution is not flexible enough.
msg68428 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-06-19 22:42
Fixed checked into trunk.
msg68434 - (view) Author: Leandro Lucarella (llucax) Date: 2008-06-20 00:26
Thank you very much
msg68435 - (view) Author: Leandro Lucarella (llucax) Date: 2008-06-20 00:41
Here is a patch to add some documentation on the new fileConfig()
argument. Is not much, but it's better than nothing =)
History
Date User Action Args
2011-04-08 10:53:57anacrolixsetnosy: + anacrolix
2008-06-20 00:41:07llucaxsetfiles: + logging.config.disable_existing_loggers.doc.patch
messages: + msg68435
2008-06-20 00:26:30llucaxsetmessages: + msg68434
2008-06-19 22:42:12vinay.sajipsetstatus: pending -> closed
resolution: not a bug -> fixed
messages: + msg68428
2008-06-19 13:02:34llucaxsetmessages: + msg68404
2008-06-19 07:59:50vinay.sajipsetstatus: open -> pending
resolution: not a bug
messages: + msg68391
2008-06-19 01:04:42benjamin.petersonsetassignee: vinay.sajip
nosy: + vinay.sajip
2008-06-19 01:02:01llucaxcreate