我目前正在像这样加载 python 记录器:
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("myprogram")
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
[...]
except FileNotFoundError:
log.exception("could not open configuration file")
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
但是,这将始终与错误消息一起打印回溯:
ERROR:myprogram:could not open configuration file
Traceback (most recent call last):
[...]
FileNotFoundError: [Errno 2] No such file or directory:
'not/existing/file.yml'
Run Code Online (Sandbox Code Playgroud)
我不想要正常错误输出中的回溯。相反,它应该只打印我的错误消息和异常信息(“没有这样的文件...”)。
仅当日志级别设置为 时,推荐的显示回溯的方式是logging.DEBUG什么?