gpt4 book ai didi

python - Django : Making exc_info optional in logging formatter

转载 作者:太空宇宙 更新时间:2023-11-03 17:57:21 24 4
gpt4 key购买 nike

我在 Django 中使用以下格式化程序来记录 django.request 模块中的警告和错误:

'format': '%(levelname)s: %(message)s\n %(exc_info)s'  

当没有发生异常时,这会打印None。当格式化程序没有异常时,是否可以跳过exc_info

这是日志记录调用:

logger.error('', exc_info=True)

最佳答案

您不需要在日志记录格式配置中显式包含 %(exc_info)s:记录异常时,将自动添加回溯和相关内容。

引用logging module的文档:

There are two keyword arguments in kwargs which are inspected: exc_info which, if it does not evaluate as false, causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) is provided, it is used; otherwise, sys.exc_info() is called to get the exception information.

(强调我的。)

另外,请关注LogRecord attributes部分:

exc_info      |      You shouldn’t need to format this yourself.

关于python - Django : Making exc_info optional in logging formatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28305851/

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