在Python日志记录HOWT中,“log=INFO”是什么意思

2024-06-08 08:47:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了Python Logging HOWTO,有一个描述我不明白:

If you want to set the logging level from a command-line option such as:

--log=INFO

and you have the value of the parameter passed for --log in some variable loglevel, you can use:

getattr(logging, loglevel.upper())

to get the value which you’ll pass to basicConfig() via the level argument.

这是什么意思?没有例子,如果有人能举一个就好了。在

我可以设置这个级别吗?在

logging --log=INFO

Tags: thetofrominfoyoulogifvalue
1条回答
网友
1楼 · 发布于 2024-06-08 08:47:27

假设您正在使用argparse之类的东西来定义命令行参数:

import argparse
p = argparse.ArgumentParser()
p.add_argument(" log")

args = p.parse_args()
loglevel = args.log

log选项的参数应该是logging模块中定义的一个级别常量;getattr(logging, loglevel.upper())将是一种获取字符串值的方法。在

相关问题 更多 >