google / google/flogger

Support for Markers in SLF4J backend

Open
#407 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.5k
Forks
133
Avg merge
6m
Merged PRs (30d)
7

Description

I'm trying to use Flogger with SLF4J+Logback as a backend so that I can output structured logs using [logstash-logback-encoder](https://github.com/logfellow/logstash-logback-encoder). With the SLF4J API, I can add structured attributes to log statements that can be parsed and efficiently searched.

I want to use the Flogger API for this, so I can take advantage of rate-limiting logging. It looks like flogger's [with(MetadataKey, Value)](https://www.javadoc.io/doc/com.google.flogger/flogger/latest/com/google/common/flogger/LoggingApi.html#with(com.google.common.flogger.MetadataKey)) would be a good fit for this. However, it seems the SFL4J-backend doesn't pass the Metadata into the SLF4J's LogEvent, but instead directly appends it to the log message. Would it be possible to pass Flogger's metadata to SLF4J and let the logging configuration choose how to format it?

Here are some concrete examples of what I'm trying to achieve in Logback and what I get with flogger:

Logback Key-value as attributes
```java
logback.atDebug().addKeyValue("key", "value").log("Logback with key values");

{"t":"2025-04-06T16:37:25","s":"INFO", "msg":"Logback with key values","attr":{"key":"value"}}
```

Logback Marker as attributes
```java
if (testLogBack.isDebugEnabled())
logback.atDebug().addMarker(append("key", "value")).log("Logback with marker");

{"t":"2025-04-06T16:37:07","s":"INFO", "msg":"Logback with marker","attr":{"key":"value"}}
```

Flogger MetaData gets appended to the formatted message
```java
flogger
.atFine()
.with(MetadataKey.single("key", String.class), "value")
.log("Flogger with key values");

{"t":"2025-04-06T16:37:25","s":"INFO", "msg":"Flogger with key values [CONTEXT key=\"value\" ]"}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.