logging.error无法捕获错误日志

时间:2018-08-28 15:26:24

标签: python logging

我正在学习日志记录模块,但对其用法非常困惑

import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
try:
    fp = open("file_not_exist.md")
    logging.error("File not exist in try")
except Exception as e:
    logging.debug("File not exist in except")

上面的代码,如果要打开的文件不存在,我想获取一条日志。

但是,它输出:

In [26]: !python test_logging.py
2018-08-28 23:15:53,835 - DEBUG - File not exist in except

logging.error无法捕获错误。

我将其修改为:

try:
    fp = open("file_not_exist.md")
    logging.debug("File not exist in try")
except Exception as e:
    logging.error("File not exist in except")

它输出:

In [28]: !python test_logging.py
2018-08-28 23:17:02,327 - ERROR - File not exist in except

输出与logging.debug相同,它无法区分“错误”或“调试”,仅打印消息。

我打算充分利用日志记录,但是看来“调试,信息,警告,错误,严重”类别只是冗长的文字。

我的应用日志记录示例代码有什么问题?

0 个答案:

没有答案