python - Python - 如何在所有日志级别甚至外部异常打印堆栈跟踪?
问题描述
我的代码库非常大,我们想打印所有日志的堆栈。
这包括所有级别的日志(甚至是 INFO 和 DEBUG)。
这还包括异常发生之外的日志。
我学到的东西:
- 堆栈跟踪不会显示,除非你在一个例外中:调用 logger.exception 时,set_exec=True,trace back.print_exc()
- 我发现显示堆栈的唯一方法是 traceback.print_stack()
- 我现在希望我的所有日志都打印出来,但我不知道如何覆盖 logger.debug/info/warning/error 来添加 traceback.print_stack()
- 这可以使用格式化程序吗?如果没有,是否可以在 logger.info() 方法发送到格式化程序之前覆盖它?
- 还有其他建议吗?
解决方案
你在正确的轨道上。我建议您阅读日志记录 HOWTO,它解释了很多事情。
您可以使用级别和过滤器来决定您的处理程序是否应该处理日志。您可以使用格式化程序将堆栈跟踪添加到日志记录消息中。
这是一个概念验证:
import logging
import traceback
import sys
logger = logging.getLogger("app.main")
class StacktraceLogFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
stack_lines = traceback.format_stack()
# we have the lines down to ^^^^^^ this call, but we don't want to show it, nor the internal logging ones
stack_lines_without_logging_intermediary_calls = filter(
lambda line: ("lib/logging/__init__.py" not in line)
and ("lib\\logging\\__init__.py") not in line,
stack_lines[:-1]
)
return record.msg + "\nOrigin :\n" + "".join(stack_lines_without_logging_intermediary_calls)
# beware of : https://stackoverflow.com/q/8162419/11384184
class InfoOrLessFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return record.levelno <= logging.INFO
def do_some_logging():
logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
try:
1/0
except ZeroDivisionError:
logger.exception("exception message")
logger.critical("critical message")
def setup_custom_logging():
logger.handlers = [] # remove the existing handlers from the logger
regular_handler_for_info_messages = logging.StreamHandler(sys.stdout)
regular_handler_for_info_messages.setLevel(logging.DEBUG) # at least DEBUG
regular_handler_for_info_messages.addFilter(InfoOrLessFilter()) # but not more than INFO
logger.addHandler(regular_handler_for_info_messages)
stacktrace_handler_for_important_messages = logging.StreamHandler(sys.stderr)
stacktrace_handler_for_important_messages.setLevel(logging.INFO + 1) # more than INFO
stacktrace_handler_for_important_messages.setFormatter(StacktraceLogFormatter())
logger.addHandler(stacktrace_handler_for_important_messages)
logger.propagate = False
def main():
logging.basicConfig(level=logging.DEBUG)
do_some_logging()
setup_custom_logging()
do_some_logging()
if __name__ == "__main__":
main()
它在之前产生:
debug message
info message
DEBUG:app.main:debug message
INFO:app.main:info message
WARNING:app.main:warning message
ERROR:app.main:error message
ERROR:app.main:exception message
Traceback (most recent call last):
File "C:/PycharmProjects/stack_overflow/67846424.py", line 43, in do_some_logging
1/0
ZeroDivisionError: division by zero
CRITICAL:app.main:critical message
之后 :
warning message
Origin :
File "C:/PycharmProjects/stack_overflow/67846424.py", line 73, in <module>
main()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 69, in main
do_some_logging()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 40, in do_some_logging
logger.warning("warning message")
error message
Origin :
File "C:/PycharmProjects/stack_overflow/67846424.py", line 73, in <module>
main()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 69, in main
do_some_logging()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 41, in do_some_logging
logger.error("error message")
exception message
Origin :
File "C:/PycharmProjects/stack_overflow/67846424.py", line 73, in <module>
main()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 69, in main
do_some_logging()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 45, in do_some_logging
logger.exception("exception message")
critical message
Origin :
File "C:/PycharmProjects/stack_overflow/67846424.py", line 73, in <module>
main()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 69, in main
do_some_logging()
File "C:/PycharmProjects/stack_overflow/67846424.py", line 46, in do_some_logging
logger.critical("critical message")
推荐阅读
- python - sqlalchemy 中的错误。列表成整数错误
- android - 如何创建渐变边框颜色
- python - 必须将 NCCL dll (Windows 10) 放在哪里才能在 Python 中使用 tensorflow?
- javascript - 反应覆盖中的模态
- android - ADB 更改系统分辨率,但不适用于应用程序
- flutter - 运行颤振时,我不断收到 pubsepc.yaml 错误
- node.js - 对引用 id 相等的文档字段求和
- google-apps-script - 如何在 Google Apps 脚本中使用全局变量?
- node.js - 在 Mongoose 中,使用保存方法重复键错误收集
- actions-on-google - 将应用操作与对话操作进行比较