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 functionality non-intuitive, levels confusing
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: jaraco, vinay.sajip
Priority: high Keywords:

Created on 2004-05-21 18:56 by jaraco, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg20860 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2004-05-21 18:56
If one tries to define additional logging levels without 
overriding the logger class, one runs into trouble with 
the log level naming conventions.  In particular, I 
observe the following strange behavior:

>>> import logging
>>> logging.basicConfig()
>>> logging.root.level
30
>>> logging.root.log( logging.INFO, 'test1' )
>>> logging.root.log( 20, 'test1' )
>>> logging.root.log( 'info', 'test1' )
Level info:root:test1
>>> logging.root.log( 'INFO', 'test1' )
20:root:test1
>>> logging.root.log( logging.WARNING, 'warning' )
WARNING:root:warning
>>> logging.root.log( 30, 'warning' )
WARNING:root:warning
>>> logging.root.log( 'warning', 'warning' )
Level warning:root:warning
>>> logging.root.log( 'WARNING', 'warning' )
30:root:warning

Note that if a string parameter is passed as the lvl 
parameter of the log() method, the message is logged 
regardless of the current logging level, and the output is 
furthermore inconsistent.

Additionally, when the user attempts to add a logging 
level, the user cannot reference that log level by name 
using any exposed (documented) interfaces.

For example:
>>> import logging
>>> logging.basicConfig()
>>> logging.root.level = logging.DEBUG # 10
>>> logging.addLevelName( 15, 'STATUS' )
>>> logging.root.log( 15, 'a status message' )
STATUS:root:a status message
>>> logging.root.log( logging._levelNames['STATUS'], 'a 
status message' )
STATUS:root:a status message

The only interface, as far as I can tell, to access the 
log level is to explicitly query the internal data 
structures.

Changes I propose:
(1) The log() methodshould either throw a TypeError 
when a string is passed for the log level or (better) it 
should accept string values and translate them 
transparently to the integer log levels that it currently 
expects.

(2) The logging module should include functionality to 
allow the user to query a logging level from a name.

def getLevelNumber( levelName ):
    return _levelNames[ levelName ]
    # error checking would be appropriate
msg20861 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2004-05-24 14:21
Logged In: YES 
user_id=308438

Point 1 - agreed.
Point 2 - getLevelName() already handles this, as it adds 
both name->value and value->name mappings to 
_levelNames. The point about type checks is valid.
msg20862 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2004-07-12 09:28
Logged In: YES 
user_id=308438

Error checking added to log() - now in CVS.
History
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40281
2004-05-21 18:56:34jaracocreate