1

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.

Andrew Rueckert
  • 4,858
  • 1
  • 33
  • 44
  • 1
    Maybe you're referring to this: https://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout ? – Roy Shahaf Aug 13 '18 at 23:05
  • I think [my answer from another question](https://stackoverflow.com/a/47242209/3284624) might help you – D.B. Aug 17 '18 at 04:14

0 Answers0