Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging - determine level number from name

Tags:

python

Python logging levels can be registered using logging.addLevelName. Is there a method to obtain the Python logging number from a level name?

like image 427
Casebash Avatar asked Aug 23 '13 00:08

Casebash


People also ask

What is logging getLogger (__ 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.

What are the five levels of logging in Python?

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.


1 Answers

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.

like image 198
abarnert Avatar answered Sep 23 '22 21:09

abarnert