Python logging levels can be registered using logging.addLevelName
. Is there a method to obtain the Python logging number from a level name?
logger = logging.getLogger(__name__) This means that logger names track the package/module hierarchy, and it's intuitively obvious where events are logged just from the logger name. Sounds like good advice.
In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.
After you call addLevelName
, the resulting level is treated exactly the same as all of the standard ones:
>>> import logging >>> logging.getLevelName(10) 'DEBUG' >>> logging.getLevelName('DEBUG') 10 >>> logging.addLevelName(15, 'DEBUGGISH') >>> logging.getLevelName(15) 'DEBUGGISH' >>> logging.getLevelName('DEBUGGISH') 15
The fact that getLevelName
can map names to numbers as well as numbers to names is not actually documented in Python 2.x, nor does the name give any hint that it should… but a quick look at the source shows why it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With