Skip to content

Commit

Permalink
make MDCHelper more testing friendly
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Dec 6, 2023
1 parent ff981fd commit 2597366
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/MDCHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.slf4j;

import org.slf4j.spi.MDCAdapter;

import java.util.HashSet;
import java.util.Set;

Expand All @@ -40,7 +42,14 @@ public class MDCHelper {
*/
Set<String> keySet = new HashSet();

MDCAdapter mdcAdapter;

public MDCHelper() {
mdcAdapter = MDC.getMDCAdapter();
}

MDCHelper(MDCAdapter mdcAdapter) {
this.mdcAdapter = mdcAdapter;
}

/**
Expand All @@ -52,7 +61,7 @@ public MDCHelper() {
* @return this instance
*/
public MDCHelper put(String key, String value) {
MDC.put(key, value);
mdcAdapter.put(key, value);
keySet.add(key);
return this;
}
Expand Down Expand Up @@ -94,7 +103,7 @@ public MDCHelper addKeys(String... keys) {
*/
public void removeSet() {
for(String key: keySet) {
MDC.remove(key);
mdcAdapter.remove(key);
}
keySet.clear();
}
Expand Down

0 comments on commit 2597366

Please sign in to comment.