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: Default handlers broken
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: jsjoseph, nnorwitz, vinay.sajip
Priority: normal Keywords:

Created on 2005-11-11 15:03 by jsjoseph, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg26851 - (view) Author: Jonathan S. Joseph (jsjoseph) Date: 2005-11-11 15:03
Hi,

There is a strange behaviour in logging. When a new
logger is created, its doesn't have any handlers...
until the root logger is invoked, at which point the
new logger uses the same handler as the root handler. 

See the following code snippet as an illustration of
the bug:

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for
more information.
>>> from logging import *
>>> z = getLogger("z")
>>> z.warning("The Larch.")
No handlers could be found for logger "z"
>>> warning("Bicycle Repair Man!")
WARNING:root:Bicycle Repair Man!
>>> z.warning("The Larch.")
WARNING:z:The Larch.
msg26852 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-11-12 01:51
Logged In: YES 
user_id=33168

This may have been fixed recently.  Vinay?
msg26853 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2005-11-12 02:51
Logged In: YES 
user_id=308438

This is not a bug, this is by design. See the documentation for 
basicConfig() in

http://docs.python.org/lib/module-logging.html

where you will see that the module-level convenience functions 
(debug(), info(), warning(), etc.) call basicConfig() automatically 
if the root logger has no handlers.

The idea is, if you are using the convenience functions, you 
will be using logging in a simple way - hence, basicConfig() is 
called for you. If you had used the root logger directly, e.g. 
after

from logging import *

by using

getLogger().warning("Bicycle Repair Man!")

rather than

warning("Bicycle Repair Man!")

then no handlers would have been added. You would not have 
received the "No handlers could be found", as that is a one-off 
message. In the latest revisions, if you set raiseExceptions to 
0 (typically for production, not development) then that 
message is suppressed completely.
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42583
2005-11-11 15:03:33jsjosephcreate