This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging module prints unexpected message when no handler set
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: roysmith, terry.reedy, vinay.sajip
Priority: normal Keywords:

Created on 2004-07-20 10:28 by roysmith, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg21717 - (view) Author: Roy Smith (roysmith) Date: 2004-07-20 10:28
I am running:

Roy-Smiths-Computer:unit$ uname -a
Darwin Roy-Smiths-Computer.local 7.4.0 Darwin Kernel Version 
7.4.0: Wed May 12 16:58:24 PDT 2004; root:xnu/xnu
-517.7.7.obj~7/RELEASE_PPC  Power Macintosh powerpc
Roy-Smiths-Computer:unit$ py
Python 2.3.4 (#3, Jun 29 2004, 21:48:03) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.

---------
#!/usr/bin/env python

import logging

logger = logging.getLogger ()
logger.error ('one')
logger.error ('two')
---------

The above program prints (to stderr):

---------
No handlers could be found for logger "root"
---------

6.28 logging --  Logging facility for Python says, "Each logger can 
have zero, one or more handlers associated with it".  It doesn't 
explicitly says what happens when there are zero handlers, but I 
would expect that any messages would just be silently ignored.  
Printing a warning to stderr is definately the WRONG thing to do.
msg21718 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2004-07-21 14:24
Logged In: YES 
user_id=308438

This is not a bug. In general, the logging module does not 
print spurious error messages - however, in the case where 
no handlers are configured at all, then this is considered a 
possible configuration error and a single message is printed. 
Note that log4j does this too - it's not peculiar to Python 
logging.

Note that logged messages are not printed - only the single 
warning indicating a possible configuration problem.
msg21719 - (view) Author: Roy Smith (roysmith) Date: 2004-07-21 14:45
Logged In: YES 
user_id=390499

I disagree.

I don't think it should be considered an error to have a logger with no 
handlers configured (especially since the documentation explicitly states 
that it's allowed).  The logic here is that every handler gets a chance to 
process the message; there's nothing magic about the list of handlers 
being empty.

Here's where this came up:  I've got a class I'm testing.  I embedded 
some logger.debug () calls in it.  Most of the time I don't even want to 
see those calls.  Sure, I could create a logger handler, and set a level 
which ignores debug calls, but that forces every user of my class to do 
that.  If a logger with no handlers attached just silently drops all 
messages, the user doesn't even have to know about the logging calls in 
my class.

Even if you were to decide to enforce that a logger must have at least 
one handler, then the error should be signified the way all other python 
errors are -- by throwing an exception.  And, of course, you should 
update the documention to match.  But, I think this would be the wrong 
approach.  Just eliminating the message to stderr would be (IMHO) the 
right thing to do.
msg21720 - (view) Author: Roy Smith (roysmith) Date: 2004-07-21 14:45
Logged In: YES 
user_id=390499

I disagree.

I don't think it should be considered an error to have a logger with no 
handlers configured (especially since the documentation explicitly states 
that it's allowed).  The logic here is that every handler gets a chance to 
process the message; there's nothing magic about the list of handlers 
being empty.

Here's where this came up:  I've got a class I'm testing.  I embedded 
some logger.debug () calls in it.  Most of the time I don't even want to 
see those calls.  Sure, I could create a logger handler, and set a level 
which ignores debug calls, but that forces every user of my class to do 
that.  If a logger with no handlers attached just silently drops all 
messages, the user doesn't even have to know about the logging calls in 
my class.

Even if you were to decide to enforce that a logger must have at least 
one handler, then the error should be signified the way all other python 
errors are -- by throwing an exception.  And, of course, you should 
update the documention to match.  But, I think this would be the wrong 
approach.  Just eliminating the message to stderr would be (IMHO) the 
right thing to do.
msg178545 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-12-30 00:10
Update: It appears that in at least 3.2+ (but not 2.7), loggers have a default handler, making the no-handler behavior moot. In any case, the example code prints 'one' and 'two' instead of the message.
History
Date User Action Args
2022-04-11 14:56:05adminsetgithub: 40603
2012-12-30 00:10:22terry.reedysetnosy: + terry.reedy
messages: + msg178545
2004-07-20 10:28:02roysmithcreate