简体   繁体   English

如何将信息级别打印到标准输出?

[英]How to print info level to stdout?

How to print info level to stdout ? 如何将信息级别打印到标准输出?

import logging
import sys
root = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)

root.info('test1')
root.error('test')

result only error level: 仅结果错误级别:

2013-11-14 11:44:19,675 - root - ERROR - test

Why ? 为什么呢

The code set the level of the handler , not logger . 该代码设置了处理程序级别 ,而不是记录 程序级别

Replace following line: 替换以下行:

ch.setLevel(logging.INFO)

with: 有:

root.setLevel(logging.INFO)

To give additional Input, 要提供其他输入,

It is something like, when we pass a message to the logger, First check happens if the logger permits the logging level, If allowed it is passed to the Handler1... Handler2, and applies the same logic. 就像这样,当我们向记录器传递消息时,首先检查记录器是否允许记录级别,如果允许,则将其传递给Handler1 ... Handler2,并应用相同的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM