Skip to content

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

Open
@rgoers

Description

@rgoers

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.

Metadata

Metadata

Assignees

Labels

bugIncorrect, unexpected, or unintended behavior of existing codeslf4jAffects SLF4J integration

Type

No type

Projects

Status

To triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions