<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<div dir="auto">Personally, I avoid `MDC.clear()` (unless there is a REALLY good reason to do so), and only mostly use `MDC.remove(...)`.</div>
<div dir="auto"><br>
</div>
<div dir="auto">And I usually practise "a class (or group of closely related classes eg have a separate before & after interceptor classes) should only ever remove what they have previously added - should not remove what other unrelated classes added (unless
 there is a REALLY good reason)". </div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> logback-user <logback-user-bounces@qos.ch> on behalf of Zac Harvey <bitbythecron@gmail.com><br>
<b>Sent:</b> Thursday, July 28, 2022 12:57:23 AM<br>
<b>To:</b> logback users list <logback-user@qos.ch><br>
<b>Subject:</b> [logback-user] MDC scopes on Spring Boot applications</font>
<div> </div>
</div>
<div>
<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-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-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-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-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-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-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-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-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-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>
</div>
</body>
</html>