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

Cha*_*Loi 2 python logging

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

\n
import logging\nlogging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)\nlogging.debug('This message should go to the log file')\nlogging.info('So should this')\nlogging.warning('And this, too')\nlogging.error('And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6')\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)\n
Run Code Online (Sandbox Code Playgroud)\n

logging只接受logging.DEBUG。那为什么info,,warning工作error

\n

小智 5

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

\n
    \n
  1. 批判的
  2. \n
  3. 错误
  4. \n
  5. 警告
  6. \n
  7. 信息
  8. \n
  9. 调试
  10. \n
  11. 没有设置
  12. \n
\n

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

\n

例子:

\n
import logging\nlogging.basicConfig(filename=\'example.log\', encoding=\'utf-8\', level=logging.DEBUG)\nlogging.debug(\'This message should go to the log file\')\nlogging.info(\'So should this\')\nlogging.warning(\'And this, too\')\nlogging.error(\'And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6\')\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
DEBUG:root:This message should go to the log file\nINFO:root:So should this\nWARNING:root:And this, too\nERROR:root:And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

例子:

\n
DEBUG:root:This message should go to the log file\nINFO:root:So should this\nWARNING:root:And this, too\nERROR:root:And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
ERROR:root:And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6\nCRITICAL:root:And non-ASCII stuff, too, like \xc3\x98resund and Malm\xc3\xb6 2\n
Run Code Online (Sandbox Code Playgroud)\n