alphanumeric
alphanumeric

Reputation: 19329

How to get logger level as string value

Using getEffectiveLevel I can query what the logger current level is:

import logging
logger = logging.getLogger('top')
level_number = logger.getEffectiveLevel()

The returned by getEffectiveLevel method value is the integer. I wonder if there is a way of querying a corresponding the string value such as "DEBUG" or "INFO" instead of an integer.

Upvotes: 9

Views: 7569

Answers (1)

stevieb
stevieb

Reputation: 9296

If you place the result of a call to the object's getEffectiveLevel() method into the logging module's getLevelName() class method, you get returned to you the string representation of the level:

import logging

log = logging.getLogger('blah')
str_level = logging.getLevelName(log.getEffectiveLevel())

print(str_level)

Output:

WARNING

Upvotes: 20

Related Questions