23

Anyone knows how structured logging is usually implemented with SLF4J?

Is there any open source already out there handling this?

thg
  • 1,209
  • 1
  • 14
  • 26
Alfred
  • 1,709
  • 8
  • 23
  • 38
  • 1
    It doesn't. SLF4j is a **logging _facade_** - it doesn't log anything. It merely provides a common API to other logging frameworks. This is a little like asking "_how does JDBC handle compound indexes?_" – Boris the Spider May 25 '16 at 07:41
  • 1
    I am the author of SLF4J. Can you explain what you mean by structured logging? – Ceki May 26 '16 at 11:00
  • 3
    @Ceki I believe structured logging in this context means added extra json fields to the log output, e.g. {"severity":"INFO", "custom-property", "test-value"} – Fritz Duchardt Feb 12 '19 at 10:07

6 Answers6

16

Slf4j added support for structured logging (and fluent API) with v2.0.0 (Alpha as of Oct 2019):

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • still in alpha ... slf4j seems dead – thg Mar 31 '21 at 16:10
  • Slf4j state is somewhat hanging. Ceki (the author) should partake project rights. It looks he keeps exclusive rights for the future and personally have no time to dedicate to the project. Everyone just migrates to Log4j 2. – gavenkoa Apr 01 '21 at 10:16
  • @thg SLF4J seems far from dead. Not only did they released v2.0.0alpha2 last month, they are also releasing updates to SLF4J 1.7 rather frequently. Moreover, SLF4J is just a simple logging facade. – RAM Aug 07 '21 at 10:25
  • 1
    @RAM The question was how slf4j does support structured logging. It does not matter if it is a facade. Also before the latest update last month the project was inactive for 18 months with no response from the owner at all. – thg Aug 11 '21 at 06:42
  • It's now 2023, and this is a perfectly acceptable answer. – Harmelodic Jul 13 '23 at 12:38
11

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"
}
Magnus
  • 7,952
  • 2
  • 26
  • 52
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
  • This Logstash Logback library is cool when your goal is Json files (when you use `net.logstash.logback.encoder.LogstashEncoder`). Unfortunately it is not compatible with Log4j or other logging frameworks and even incompatible with other Logback formatters. It only works with own supplied formaters (( – gavenkoa Apr 13 '22 at 22:57
4

There is an example in github which is implemented using SLF4J. Hope it will help you.

For more learning you can go through this tutorial.

  1. Structured logging in logback using MDC
  2. Master Java Logging
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
4

FYI - I've just open sourced a structured logging wrapper for SLF4J. We're using it at my day job to front logging into Splunk and it's been a big improvement. Maybe you will find it useful.

https://github.com/Randgalt/maple

You define a "schema" and then wrap an SLF4J logger. E.g.

public interface LoggingSchema {
    LoggingSchema name(String name);

    LoggingSchema date(Instant date);

    ... etc ...
}    

...

MapleLogger<LoggingSchema> logger = MapleFactory.getLogger(slf4j, LoggingSchema.class);
logger.info("my message", s -> s.name("john doe").date(Instant.now());

Generates an slf4j log ala:

slf4j.info("my message name=\"john doe\" date=2019-10-08T18:52:15.820177Z");
Randgalt
  • 2,907
  • 1
  • 17
  • 31
2

For anyone who stumbles upon this rather old question: As an alternative approach that is way more developer friendly than manual setting of each param into MDC, you could use a structured logger like https://github.com/jacek99/structlog4j ideally along with yaml or json formatter. Then it is really easy to feed the logs to ELK stack, and query all logs based on parameters(you wont have to create log entry parser manually, as all relevant fields will be there in structured form already). Or create your own simple logger on top of slf4j, that would take any varargs passed the the .log method and automatically create key-value pairs in MDC, and then you can pair it with a structured formatter e.g. if your runtime uses Logstash: https://github.com/logstash/logstash-logback-encoder#mdc-fields

yntelectual
  • 3,028
  • 18
  • 24
-1

You may try to user Logstage in Scala https://izumi.7mind.io/latest/release/doc/logstage/index.html

We have an effectful adapter for slf4j and we perform zero-cost structuring for your logs.

Also, you may find that we have many advantages despite MDC replacing. For effect libs, we have a context for Fiber and FiberLocal We have automatic structure identifiers for it's better processing in structured databases