Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Log4j2's MultiFormatStringBuilderFormattable in structured logging #42034

Closed
mhalbritter opened this issue Aug 27, 2024 · 3 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@mhalbritter
Copy link
Contributor

mhalbritter commented Aug 27, 2024

At the moment, this code

LOG4J.info(new MapMessage<>().with("foo", true).with("bar", 1.0));

leads to this output:

{"@timestamp":"2024-08-27T14:05:28.609449Z","log.level":"INFO","process.pid":13608,"process.thread.name":"main","service.name":"structured-logging-playground","log.logger":"com.example.structuredloggingplayground.CLR","message":"bar=\"1.0\" foo=\"true\"","ecs.version":"8.11"}

Instead, it should add "foo" and "bar" as separate keys and also preserve the datatypes.

@mhalbritter mhalbritter changed the title ERnable Add support for Log4j2's MapMessage in structured logging Aug 27, 2024
@mhalbritter mhalbritter added the type: enhancement A general enhancement label Aug 27, 2024
@mhalbritter mhalbritter added this to the 3.4.x milestone Aug 27, 2024
@mhalbritter
Copy link
Contributor Author

We should also see if there are more subtypes of Message which are suitable for structured logging.

@philwebb philwebb changed the title Add support for Log4j2's MapMessage in structured logging Add support for Log4j2's MultiFormatStringBuilderFormattable in structured logging Aug 27, 2024
@philwebb
Copy link
Member

I think we have couple of options here, we could call MapMessage.getData() and use our JsonWriter to write the result, or we could use MultiFormatStringBuilderFormattable and let it write the JSON.

I took a stab at the second option in https://github.com/philwebb/spring-boot/tree/gh-42034 if you want to take a look @mhalbritter.

@mhalbritter
Copy link
Contributor Author

Thanks Phil, that looks good!

@philwebb philwebb self-assigned this Aug 28, 2024
@philwebb philwebb modified the milestones: 3.4.x, 3.4.0-M3 Aug 29, 2024
philwebb added a commit that referenced this issue Aug 29, 2024
Update Log4J2 `ElasticCommonSchemaStructuredLogFormatter` and
`LogstashStructuredLogFormatter` to support Log4J2 JSON structured
messages (typically `MapMessage`)

Closes gh-42034
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants