gpt4 book ai didi

python - `logging.basicConfig(level=logging.INFO)` 是否应该只接受 INFO 级别的日志记录?

转载 作者:行者123 更新时间:2023-12-05 09:36:02 30 4
gpt4 key购买 nike

下面是来自源代码的示例代码:https://docs.python.org/3/howto/logging.html

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

我认为下面代码的level=logging.DEBUG

logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)

使 logging 仅接受 logging.DEBUG。那为什么info, warning, error work

最佳答案

python的日志记录级别按以下顺序排列:

  1. 关键
  2. 错误
  3. 警告
  4. 信息
  5. 调试
  6. 未设置

如果您在配置为 logging.DEBUG 时设置根记录器级别,它将写入所有级别高于该级别的日志。

例子:

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

输出:

DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too
ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö

如果在配置为 logging.ERROR 时设置根记录器级别,它将只写入 CRITICAL 日志和 ERROR 日志。

例子:

import logging
logging.basicConfig(filename='example.log', level=logging.ERROR)
logging.warning('this is warning')
logging.info('this is info')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
logging.critical('And non-ASCII stuff, too, like Øresund and Malmö 2')

输出:

ERROR:root:And non-ASCII stuff, too, like Øresund and Malmö
CRITICAL:root:And non-ASCII stuff, too, like Øresund and Malmö 2

关于python - `logging.basicConfig(level=logging.INFO)` 是否应该只接受 INFO 级别的日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65418560/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com