title: Is it possible to format the MDC/Context Map as JSON using a PatternLayout in Log4j2?
body: <p>I'm using log4j2 with a PatternLayout, and this pattern: <code>%d %-5p %t %c{1.} - %m %X%n%ex</code>. That <code>%X</code> appends the Context Map (aka Media Diagnostic Context) to the end of the log line, before any exceptions. </p>
  
  <p>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 <code>Map.toString</code> implementation - since it's <code>{key1=value 1, key2=value 2}</code> with no escaping, it becomes difficult to parse when a value contains an unescaped <code>=</code> or <code>,</code>. JSON, on the other hand, would be <em>trivial</em> to parse back into a map.</p>
  
  <p>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.</p>