using MDC I wanted print both key and value.

With

MDC.put("key", value);

and the log4j pattern :

<pattern>%date{yyyy/MM/dd HH:mm:ss.SSS} %5level - %mdc{KEY} </pattern>

I have this result :

2019/04/11 16:25:52.983 WARN - **value**

but I expected

2019/04/11 16:25:52.983 WARN - **key=value**

my first idea was to append the key in the value but it's not very clean

MDC.put("key", "key " + value);

does anyone have a solution?

Thx

edit:

adding the key statically in the pattern is not a good solution

<pattern>%date{yyyy/MM/dd HH:mm:ss.SSS} %5level - key=%mdc{KEY} </pattern>

sometimes the value doesn't exist and it will print 'key='

0 Answers