Search code examples
javaslf4jlogstash-logback-encoder

Logging exceptions together with structured arguments


Does anyone know what are best practices for logging exceptions together with structured arguments? Looking at https://github.com/logstash/logstash-logback-encoder#customizing-stack-traces, it is suggested to not use them but no alternative is provided.


Solution

  • Just log exceptions as you would normally log exceptions with slf4j/logback. Specifically, provide the exception as the last argument to the log statement.

    Examples

    // 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);