<div dir="ltr"><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">I'm interested in utilizing the<span> </span><a href="https://logback.qos.ch/manual/mdc.html" rel="nofollow noreferrer" style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit;text-decoration:underline">Mapped Diagnostic Context (<code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">MDC</code>)</a><span> </span>but have two types of MDC needs:</p><ul style="margin-top:0px;margin-right:0px;margin-left:30px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;list-style:outside;box-sizing:inherit;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><li style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit"><strong style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:bold;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">global</strong>: I need to add certain key-value pairs to the MDC when the server starts up, and I need them present in every log message for the entire life of the server; and</li><li style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit"><strong style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:bold;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">request-scoped</strong><span> </span>(thread scoped?): certain key-value pairs need to be added to the MDC but will change for every/most HTTP request the server receives</li></ul><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">I know that<span> </span><code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">MDC</code><span> </span>is thread-safe, so I'm guessing each HTTP request will have its own "blank slate" version/instance of the MDC to work with, which takes care of my request-scoped needs. But from inside request handlers (servlet filters, Spring controllers, etc.) I still need the global MDC key-value pairs present in any log messages that are made.</p><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">For example, say, at server startup I need to add:</p><pre style="margin-top:0px;margin-right:0px;margin-left:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial"><code style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit;border-radius:0px">MDC.put("Server-Instance-Id", serverInstanceId);
</code></pre><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">I will need "<em style="margin:0px;padding:0px;border:0px none;font-style:italic;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">Server-Instance-Id</em>" in every single log message sent, regardless of what thread/request the log message is sent from.</p><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">But then say in my<span> </span><code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">FizzController#getFizzes()</code><span> </span>method, which accepts HTTP requests to, say,<span> </span><code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">GET /v1/fizzes</code>, I have:</p><pre style="margin-top:0px;margin-right:0px;margin-left:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial"><code style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit;border-radius:0px">MDC.put("Fruit", pear.getName());
<a href="http://logger.info">logger.info</a>("User is requested all fizzes");
</code></pre><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">In this case above, I would want<span> </span><strong style="margin:0px;padding:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:bold;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">both</strong><span> </span>"<em style="margin:0px;padding:0px;border:0px none;font-style:italic;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">Server-Instance-Id</em>" and "<em style="margin:0px;padding:0px;border:0px none;font-style:italic;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">Fruit</em>" values included in the log message.</p><p style="margin-top:0px;margin-right:0px;margin-left:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Similarly, if I clear the MDC (<code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">MDC.clear()</code>) from inside a request thread (such as a call to<span> </span><code style="margin:0px;border:0px none;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;white-space:pre-wrap">FizzController#getFizzes()</code>), I don't want it to clear the global context variables (such as "<em style="margin:0px;padding:0px;border:0px none;font-style:italic;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;font-size:15px;vertical-align:baseline;box-sizing:inherit">Server-Instance-Id</em>"). Only clear the request-scoped ones.</p><p style="margin:0px;padding:0px;border:0px none;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-weight:400;font-stretch:inherit;line-height:inherit;font-family:-apple-system,"system-ui","Segoe UI Adjusted","Segoe UI","Liberation Sans",sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(35,38,41);letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Does anyone know how to accomplish this with SLF4J config or its API?</p></div>