In JSON Template Layout, can you handle messages differently based on their type? #3122
Unanswered
LoganDev99
asked this question in
Q&A
Replies: 2 comments 6 replies
-
@LoganDev99, I think what you're looking for is <JsonTemplateLayout eventTemplate='{"$resolver": "message", "fallbackKey": "literal"}'/> below Java code LOGGER.info("plain text");
LOGGER.info(new MapMessage<>().with("key", "value")); outputs
Does this help? |
Beta Was this translation helpful? Give feedback.
3 replies
-
@LoganDev99, how do you expect a |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I posted a StackOverflow, but forgot about this discussion model in GitHub.
https://stackoverflow.com/questions/79120207/how-do-you-log-a-message-if-your-event-template-expects-a-map-but-doesnt-receiv
--- Copying Message to here ---
I want to be able to use Log4j2 to log in JSON format. I am using JsonTemplateLayout and my own Event Template. My application uses MapMessage when logging so I can extract out values for my log format.
The problem is that not all logs are coming from my source code, some could be from Spring or other libraries that may not be sending a MapMessage.
The problem is that when these libraries are logging without MapMessage my JSON EventTemplate doesn't extract out the message to log.
Full Code Available: https://github.com/LoganDev99/log4j2map
Java Logging Snippet - Log with MapMessage
log4j2.xml - Using JsonTemplateLayout and additivity false.
MyLayout.json (Event Template) - Using map resolver
Console Output
Notice how in the first 3 logs there's is no message because in this case it's Spring logging. Spring logs such as "Started Log4j2mapApplication in 1.3 seconds" for example.
So my question is how do have a fallback to just log the message if the message isn't a map.
Beta Was this translation helpful? Give feedback.
All reactions