Anyone knows how structured logging is usually implemented with SLF4J?
Is there any open source already out there handling this?
Bindings are basically implementations of a particular SLF4J class meant to be extended to plug in a specific logging framework. By design, SLF4J will only bind with one logging framework at a time. Consequently, if more than one binding is present on the classpath, it will emit a warning.
SLF4J based implementation of commons-logging wrapper APIs.
Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.
So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.
If you use SLF4J in conjunction with Logback and Logstash, structured logging is supported with StructuredArguments
. You can find documentation about this on the logstash logback encoder page on Github.
A simple example of how it works. This log line..
log.debug("Retrieved file {}", StructuredArguments.value("filename", upload.getOriginalFilename()))
..yields the following log json output:
{
"filename": "simple.zip",
"@timestamp": "2019-02-12T14:31:31.631+00:00",
"severity": "DEBUG",
"service": "upload",
"thread": "http-nio-9091-exec-1",
"logger": "some.great.ClassName",
"message": "Retrieved file simple.zip"
}
Slf4j added support for structured logging (and fluent API) with v2.0.0 (Alpha as of Oct 2019):
http://www.slf4j.org/apidocs/org/slf4j/spi/LoggingEventBuilder.html
int newT = 15;
int oldT = 16;
// using classical API
logger.debug("oldT={} newT={} Temperature changed.", newT, oldT);
// using fluent API
logger.atDebug()
.addKeyValue("oldT", oldT)
.addKeyValue("newT", newT)
.log("Temperature changed.");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With