Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print current logging level

Tags:

python

logging

Here's a bit of code I'm running under:

import logging

logger = logging.getLogger("test.logger")
logger.setLevel(logging.DEBUG)

print("Effective logging level is {}".format(logger.getEffectiveLevel()))

And here's the output:

Effective logging level is 10

How do I print the level instead of the number?

like image 227
Sten Kin Avatar asked Apr 29 '14 15:04

Sten Kin


People also ask

How do you find the current log level?

The getLevel() method of the Logger class in Java is used to get the log Level that has been specified for this Logger instance. Every Logger has specific log levels and if the result is null, which means that this logger's effective level will be inherited from its parent.

How do I print logging INFO in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.

What is logger set level?

The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on."


1 Answers

Pass the numeric level to logging.getLevelName():

>>> import logging
>>> logging.getLevelName(10)
'DEBUG'

Does what it says on the tin:

Returns the textual representation of logging level lvl.

For your code that'd be:

print("Effective logging level is {}".format(
    logging.getLevelName(logger.getEffectiveLevel())))
like image 144
Martijn Pieters Avatar answered Sep 20 '22 14:09

Martijn Pieters