How does SLF4J support structured logging [closed]
Asked Answered
I

6

23

Anyone knows how structured logging is usually implemented with SLF4J?

Is there any open source already out there handling this?

Ichthyic answered 25/5, 2016 at 7:36 Comment(3)
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?"Flacon
I am the author of SLF4J. Can you explain what you mean by structured logging?Triley
@Triley I believe structured logging in this context means added extra json fields to the log output, e.g. {"severity":"INFO", "custom-property", "test-value"}Handily
A
22

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

Arron answered 5/11, 2019 at 11:9 Comment(4)
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.Arron
@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.Dunnage
@Dunnage 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.Neal
It's now 2023, and this is a perfectly acceptable answer.Intricacy
H
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"
}
Handily answered 12/2, 2019 at 14:36 Comment(1)
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 ((Arron
T
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
Trona answered 25/5, 2016 at 7:55 Comment(0)
N
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");
Narwhal answered 8/10, 2019 at 18:53 Comment(0)
R
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

Robinia answered 11/5, 2018 at 6:40 Comment(0)
E
-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

Ezana answered 28/3, 2019 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.