I'm using log4j2 with a PatternLayout, and this pattern: %d %-5p %t %c{1.} - %m %X%n%ex
. That %X
appends the Context Map (aka Media Diagnostic Context) to the end of the log line, before any exceptions.
In my log aggregator, I'm trying to parse these messages back into structured data - it's easy for the date, level, time, etc, but it's harder to piece the map back together from Java's default Map.toString
implementation - since it's {key1=value 1, key2=value 2}
with no escaping, it becomes difficult to parse when a value contains an unescaped =
or ,
. JSON, on the other hand, would be trivial to parse back into a map.
I can't find anywhere to hook into the PatternLayout's MDC-printing, but Log4j2 is a very extendable library, and I'm hoping that there's something that I've overlooked.