apache / apache/logging-log4j2

Logs generated using SLF4J's addKeyValue method do not log correctly.

Open
#1,813 7 comments 4 reactions 1 assignee Claimed by @rgoers View on GitHub
bug slf4j
Dominant language
Java
Stars
3.6k
Forks
1.7k
Avg merge
21h 30m
Merged PRs (30d)
27

Description

## Description

See https://jira.qos.ch/browse/SLF4J-600. It seems SLF4J added an addKeyValue method to its fluent API. When this is called the data is not being added to a MapMessage as it needs to be.

## Configuration

**Version:** 2.20.0

## Logs

When SLF4J routes to Log4j the output looks like:
```
[19.09.2023 19:25:51,248] [INFO ] - slf4j2-with-log4j - Measure times:
```

None of the key/value pairs are present.

## Reproduction

The code has

```
private static final Logger LOG = LoggerFactory.getLogger("PERFORMANCE");
...

LOG.atInfo().setMessage("slf4j2-with-log4j - Measure times:")
.addKeyValue("myDocId", documentId)
.addKeyValue("myInitExtractorInMs", createFormExtractor)
.addKeyValue("myvalidatedDocumentInMs", validatedDocument)
.addKeyValue("myreadPageInMs", readPage)
.addKeyValue("mytransformInfosInMs", transformInfos)
.log();
```

To work properly this needs to translate into

```
StringMapMessage log4jMap = new StringMapMessage()
.with("message", "direct log4j implementation - Measure times:")
.with("myDocId", documentId)
.with("myInitExtractorInMs", createFormExtractor)
.with("myvalidatedDocumentInMs", validatedDocument)
.with("myreadPageInMs", readPage)
.with("mytransformInfosInMs", transformInfos);
LogManager.getLogger("PERFORMANCE").info(log4jMap);

```

Note that IMO it would be better for Log4j's LogBuilder add support for addKeyValue and internally populate a MapMessage.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.