Logging exceptions together with structured arguments
Asked Answered
I

1

5

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.

Industry answered 8/1, 2020 at 13:55 Comment(0)
M
10

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);
Matchwood answered 8/1, 2020 at 17:56 Comment(1)
It works, I made the mistake of putting the exception before the structured arguments and not tried the opposite. Thanks for the help!Industry

© 2022 - 2024 — McMap. All rights reserved.