rgoers commented on pull request #770:
URL: https://github.com/apache/logging-log4j2/pull/770#issuecomment-1072974525
Ok. So your big mistake here is trying to do this with the SLF4J API. It
wasn't designed to support structured event logging. I fought for that years
ago and gave up and that was one of the reasons I created Log4j 2. You really
want to use a MapMessage or extend it. I have teams that are doing exactly this
to capture data to display in Kibana dashboards. WE still use Markers, but the
Marker simply identifies it a) as an event and b) the type of event. The second
could have been captured in the event itself.
This is the code that logs generates the event.
```
public void recordInbound(Inbound in, Vendors vendor) {
InboundLogEntry inboundLogEntry = new InboundLogEntry();
inboundLogEntry.setVendor(vendor.name());
inboundLogEntry.setInbound(true);
inboundLogEntry.setDestination(in.getDestination());
inboundLogEntry.setRefId(in.getId());
inboundLogEntry.setMediaUrls(in.getMediaUrls());
inboundLogEntry.setSource(in.getSource());
inbound.ogEntry.setMessageId(in.getMessageId());
inboundLogEntry.logEvent();
}
```
and here is the logEvent method.
```
public void logEvent() {
StackTraceElement element = StackLocatorUtil.getStackTraceElement(2);
LOGGER.atInfo().withLocation(element).withMarker(INBOUND_EVENT).log(this);
}
```
Note that a detached marker is not required as all the variable data is in
the event.
Finally, in the logging configuration we use JsonTemplateLayout and include
in the template
```
"event.action": {
"$resolver": "marker",
"field": "name"
},
"event.data": {
"$resolver": "map",
"stringified": true
},
```
So the Marker will be logged as the event.action field and all the data will
be included as attributes under event.data - such as event.data.vendor,
event.data.destination, etc.
So this is exactly why I am not in favor of merging this PR as the Log4j API
is much richer than SLF4J. You do not need to convert your whole application to
use SLF4J but you should modify anything generating structured events.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]