David542
David542

Reputation: 110257

Getting the log level in python

After setting the level to info:

import logging
logging.basicConfig(filename='example.log',level=logging.INFO)

Is there a way I can print the level to confirm it's set to logging.INFO ? I was hoping logging.__dict__ would show it, but I can't find it from that dict.

Upvotes: 1

Views: 404

Answers (2)

Max Power
Max Power

Reputation: 8954

Would this work for you?

import logging
lvl = logging.INFO

logging.basicConfig(filename='example.log',level=lvl)    
print(logging.getLevelName(lvl))
>>>INFO

Upvotes: 0

mechanical_meat
mechanical_meat

Reputation: 169334

You can do it this way:

>>> logging.getLevelName(logging.getLogger().getEffectiveLevel())
'INFO'

The call to .getEffectiveLevel() returns the numeric code of the level, and from there you get the level name via the appropriately named .getLevelName().

Upvotes: 2

Related Questions