将异常与结构化参数一起记录

gma*_*tti 5 java slf4j logstash-logback-encoder

有谁知道记录异常和结构化参数的最佳实践是什么?查看https://github.com/logstash/logstash-logback-encoder#customizing-stack-traces,建议不要使用它们,但没有提供替代方案。

Phi*_*lay 9

只需记录异常,就像通常使用 slf4j/logback 记录异常一样。具体来说,将异常作为日志语句的最后一个参数提供。

例子

// With no other arguments
logger.warn("Something bad happened", exception);

// With a regular (non-structured) argument
logger.warn("Something bad happened with {}", "foo", exception);

// With a structured argument
logger.warn("Something bad happened with {}", kv("foo", "bar"), exception);
Run Code Online (Sandbox Code Playgroud)