【问题标题】:Logging exceptions together with structured arguments将异常与结构化参数一起记录
【发布时间】:2020-01-08 13:55:57
【问题描述】:

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

【问题讨论】:

    标签: java slf4j logstash-logback-encoder


    【解决方案1】:

    只需像 normally log exceptions with 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);
    

    【讨论】:

    • 它有效,我犯了将异常放在结构化参数之前的错误,而没有尝试相反的方法。感谢您的帮助!
    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2021-01-16
    • 2021-08-03
    相关资源
    最近更新 更多