Search code examples
javalog4j2mdc

log4j2.xml file has KeyValuePair set in MDC. If key is not set, how can we omit the value altogether from the log line?


If I have a KeyValuePair in my JsonLayout appender, and that pair is not set in the MDC, is there a way to exclude the value from the logs?


Solution

  • In Java,

    You can use ':-' for null value in log4j2.xml

    Example:

    <KeyValuePair key="traceId" value="$${ctx:traceId:-}"/>