gmariotti
gmariotti

Reputation: 519

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.

Upvotes: 5

Views: 2747

Answers (1)

Phil Clay
Phil Clay

Reputation: 4534

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

Upvotes: 10

Related Questions