How does SLF4J support structured logging

Alfred picture Alfred · May 25, 2016 · Viewed 16.6k times · Source

Anyone knows how structured logging is usually implemented with SLF4J?

Is there any open source already out there handling this?

Answer

Fritz Duchardt picture Fritz Duchardt · Feb 12, 2019

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"
}