我有以下简单的测试脚本:
import logging
import sys
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'$
logger.addHandler(handler)
logger.info('Hello World!')
logger.warning('Bad!')
python test.py
我在命令行中运行这个脚本。我得到:
__main__ - WARNING - Bad!
当我期望得到:
__main__ - WARNING - Bad!
__main__ - INFO - Hello World!
例如,我希望 theinfo
和warning
log 消息都打印到控制台窗口,但只有warning
消息打印。怎么会?