Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value against an MDC key in log4j 2?

As in log4j we have an option to set a default value against an MDC key like this - mdc{key:-defaultVal}

Do we have something similar in log4j 2 ?

like image 1000
Tanmoy Avatar asked Nov 23 '16 15:11

Tanmoy


2 Answers

Looking in MdcPatternConverter it does not have support for default value.

There is open Jira ticket on that Tickt

I find that you can also use this: ${ctx:<key>:-<default_value>}

like image 78
igreenfield Avatar answered Oct 01 '22 00:10

igreenfield


While there is no way to set a default with the %X pattern, there are the %equals and %equalsIgnoreCase patterns which can be used for something equivalent.

%equals{%X{<key>}}{}{<default>}

like image 32
bnorm Avatar answered Oct 01 '22 02:10

bnorm