Bugs item #1164953, was opened at 2005-03-17 02:38
Message generated for change (Comment added) made by vsajip
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1164953&group_id=5470

Category: Python Library
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: logistix (logistix)
Assigned to: Vinay Sajip (vsajip)
Summary: logging.basicConfig creates phantom handler

Initial Comment:
calling logging.basicConfig() creates a phantom handler 
that doesn't operate in an intuitive way.  A reproducable 
follows.

My actual use case:  I started off using the logging 
module on a project with the builting logging.info(), 
logging.debug(), logging.error() functions.  As my needs 
got more sophisticated, I started creating some loggers 
via logging.getLogger('a.b.c')  I setup a custom handler 
to do formatting without printing "INFO:a.b.c" headers.  
One of my import statements triggered a call to 
logging.basicConfig.  Halfway through the running 
program, my logging messages started showing up 
twice.  Once without the header and once with.  I 
eventually tracked this down to usage of a logging.info() 
statement.  I tried explicitly calling logging.basicConfig
(level=logging.ERROR) to disable the duplicate errors, 
but that didn't work.

A trivial patch is attached.  It fixes the problem if you 
explicitly call logging.basicConfig.  I don't know if it will 
fix the implicit calls by logging.info(), etc.

C:\Python24>python
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more 
information.
>>> import logging
>>>
>>> logger = logging.getLogger('foo')
>>> logger.setLevel(logging.INFO)
>>> logger.info('bar') # no output yet
No handlers could be found for logger "foo"
>>>
>>> console = logging.StreamHandler()
>>> console.setLevel(logging.INFO)
>>> console.setFormatter(logging.Formatter("%
(message)s")
... )
>>> logger.addHandler(console)
>>>
>>> logger.info('foo')
foo
>>>
>>> logger.warning('foo')
foo
>>>
>>> logger.debug('foo')
>>>
>>> logger.info('foo')
foo
>>>
>>> logging.basicConfig(level=logging.ERROR)
>>>
>>> logger.info('foo')
foo
INFO:foo:foo
>>> ^Z


C:\Python24>

----------------------------------------------------------------------

>Comment By: Vinay Sajip (vsajip)
Date: 2005-03-17 10:52

Message:
Logged In: YES 
user_id=308438

Thanks for the feedback, but it's not a bug. Check the 
docstring for basicConfig: "This function does nothing if the 
root logger already has handlers configured. It is a 
convenience method intended for use by simple scripts to do 
one-shot configuration of the logging package. The default 
behaviour is to create a StreamHandler which writes to 
sys.stderr, set a formatter using the BASIC_FORMAT format 
string, and add the handler to the root logger."

The logging.debug(), etc. methods are for simple use of the 
logging package. If you are using named loggers, then don't 
use basicConfig() except to configure a handler at the root 
level - and do this early in your application. Thereafter you 
can log to the root logger (using logging.debug() and its 
brethren) or to a named logger which has no handlers set (the 
root's handlers will be used).

If you add a console handler manually to logger "foo" and 
then add another one to the root handler via basicConfig(), 
then both handlers will be called (correctly). This is what you 
are seeing.

Your fix (add the level to the handler as well) does not fit the 
contract of basicConfig(). Users working in simple mode will 
just use the logger's level. Setting levels in the handler is for 
more sophisticated use of the logging package than implied 
by use of basicConfig() and logging.debug(), etc.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1164953&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to