在调用logger.setLevel时使用变量 [英] Using a variable while calling logger.setLevel
问题描述
有人知道Python的Logging模块的setlevel()函数中是否有使用变量的方法?
Does anyone know if there is a way to use a variable in the setlevel() function of Python's Logging module?
此刻我正在使用它:
Log = logging.getLogger('myLogger')
Log.setLevel(logging.DEBUG)
但是我想要这个:
Log = logging.getLogger('myLogger')
levels = {'CRITICAL' : logging.critical,
'ERROR' : logging.error,
'WARNING' : logging.warning,
'INFO' : logging.info,
'DEBUG' : logging.debug
}
level = levels['INFO']
Log.setLevel(level)
但是它似乎不起作用-它只是不记录任何内容.
But it doesn't seem to work - it just doesn't log anything.
我这样做是为了可以从单个配置文件中的变量设置一堆脚本的日志记录级别.
I'm doing this so that I can set the logging level for a whole bunch of scripts from a variable in a single config file.
推荐答案
您还应该能够做到这一点:
You should also be able to do this:
Log = logging.getLogger('myLogger')
level = logging.getLevelName('INFO')
Log.setLevel(level)
logging.getLevelName(lvl)
函数可双向运行.我使用它,它可以正常工作(尽管您应该检查python的实现).
The logging.getLevelName(lvl)
function works both ways. I use it, it works (you should check your python implementation though).
这为您节省了维护自己的词典的麻烦,并减少了打字错误的可能性.
This saves you the trouble to maintain your own dictionary, and reduces the possibility of typo errors.
这篇关于在调用logger.setLevel时使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!