Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logging: How to pass logging level as an argument

Tags:

python

logging

How can we pass logging level as an argument?

For example:

level1 = 'DEBUG'
level1lower = level1.lower()
logger.setLevel(logging.level1)
logger.level1lower('Some messages passed for logging's)

This is the closest representation of my problem coz I want to write one function that would take logging levels as argument.

like image 599
robinhoodjr Avatar asked Jul 30 '26 00:07

robinhoodjr


1 Answers

Use logger.log instead of the methods that supply an implicit log level. You'll have to supply the integer logging level, though.

# Equivalent to logger.debug(msg)
level1 = 'DEBUG'
logger.log(getattr(logging, level1),  # Convert DEBUG to 10
           msg)
like image 105
chepner Avatar answered Jul 31 '26 14:07

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!