Quick and hacky way:
You can use eval()
. It executes python code in strings. (Docs)
Example:
import logging
logging.basicConfig(
level=eval('logging.INFO')
)
logging.info('Works!')
Outputs:
INFO:root:Works!
Although this works, using eval
is considered bad practice because you can execute any Python code so it's not secure i.e. can pass anything in config and it will be executed in your application. More info here.
Safer approach:
In your config file you can pass 'your' defined level keywords such as info
or warning
etc. (All levels)
Example config:
log:
filename: 'log'
format: '%(levelname)s:%(message)s'
level: 'info'
In code, you can create a mapping between those words and actual logging level and then instantiate the logger with that.
import logging
#Create mappings
level_mapping = {'info': logging.INFO, 'warning': logging.WARNING}
#Read your config file
config = {}
#When passed logging level doesn't exist in mappings, raise an exception
if config['log']['level'] not in level_mapping:
raise ValueError('Invalid logging level passed in config!!!')
#Instantiate
logging.basicConfig(
level=level_mapping[config['log']['level']]
)
This way you have a whitelist of accepted levels in your application.