7

I wonder what the best practices are with using StructuredArguments inside logging calls when using logstash-logback-encoder to log in JSON format.

I want to log some structured arguments in separate fields, but I don't want to format these arguments into the literal string message.

If I write my log lines like this, everything works fine as I want to, but both my IntelliJ IDEA and Sonarqube static code analysis considers this problematic issues:

log.info("Query executed successfully!", StructuredArguments.value("hits", result.getHits()));
(or more concise)
log.info("Query executed successfully!", v("hits", result.getHits()));

IntelliJ warns this on this line:

more arguments provided (1) than placeholders specified (0)

How can I avoid this? Of course I can silence the warnings and add exceptions for them, but I wonder if that is a best practice.

ChrisDekker
  • 1,584
  • 18
  • 39

1 Answers1

11

If you don't want to include the data inside the log message, and want to avoid the static analysis warnings, use Markers instead of StructuredArguments:

import net.logstash.logback.marker.Markers;

log.info(Markers.append("hits", result.getHits()), "Query executed successfully!");

See here for more details.

Phil Clay
  • 4,028
  • 17
  • 22