Logbback xml - Conditionally add MDC Variables if they exist?

20 views Asked by At

I have configured my logback to look like this:

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [env=${environment}] [app=${app.name}] [version=${version.version}] [level=%level] [thread=%thread] [logger=%logger] [X-REQUEST-ID=%mdc{X-REQUEST-ID:--}] %msg %n</pattern>

What this does is that if it doesn't find X-REQUEST-ID in context then it logs like this:

Currently: yyyy-MM-dd HH:mm:ss.SSS [env=dev] [app=sample] [version=2] [level=INFO] [thread=...] [logger=...] [X-REQUEST-ID=-]

however, I want to eliminate the complete statement: [X-REQUEST-ID=-]

Expected:

yyyy-MM-dd HH:mm:ss.SSS [env=dev] [app=sample] [version=2] [level=INFO] [thread=...] [logger=...] (Nothing at all for X-RE

and let other parameters be printed if X-REQUEST-ID doesn't get detected in MDC context.

Can someone help me rewrite the pattern block in that case?

0

There are 0 answers