Skip to content

Commit

Permalink
rename MDCHelper as MDCAmbit, rename removeSet() method as clear()
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Dec 18, 2023
1 parent 98812ca commit 50b9f5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
40 changes: 20 additions & 20 deletions slf4j-api/src/main/java/org/slf4j/MDCAmbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@
* <p>Typical Usage example:</p>
*
* <pre>
* MDCHelper mdch = new MDCHelper();
* MDCAmbit mdca = new MDCAmbit();
* try {
* mdch.put("k0", "v0");
* mdca.put("k0", "v0");
* throw new RuntimeException();
* } catch (RuntimeException e) {
* // here MDC.get("k0") would return "v0"
* } finally {
* // MDC remove "k0"
* mdch.removeSet();
* mdca.clear();
* }
* </pre>
*
* <p>It is also possible to chain {@link #put} invocations. For example:</p>
* <pre>
* MDCHelper mdch = new MDCHelper();
* MDCAmbit mdca = new MDCAmbit();
* try {
* // assume "k0" was added to MDC at an earlier stage
* mdch.addKey("k0").put("k1", "v1").put("k2, "v2");
* mdca.addKey("k0").put("k1", "v1").put("k2, "v2");
* } finally {
* // MDC remove "k0", "k1", "k2"
* mdch.removeSet();
* // MDC remove "k0", "k1", "k2", clear the set of tracked keys
* mdch.clear();
* }
* </pre>
* @since 2.0.10
* @since 2.1.0
*/
public class MDCHelper {
public class MDCAmbit {


/**
Expand All @@ -70,44 +70,44 @@ public class MDCHelper {

MDCAdapter mdcAdapter;

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

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

/**
* Put the key/value couple in the MDC and keep track of the key for later
* removal by a call to {@link #removeSet()}.
* removal by a call to {@link #()}.
*
* @param key
* @param value
* @return this instance
*/
public MDCHelper put(String key, String value) {
public MDCAmbit put(String key, String value) {
mdcAdapter.put(key, value);
keySet.add(key);
return this;
}

/**
* Keep track of a key for later removal by a call to {@link #removeSet()}.
* Keep track of a key for later removal by a call to {@link #()}.
* @param key
* @return this instance
*/
public MDCHelper addKey(String key) {
public MDCAmbit addKey(String key) {
keySet.add(key);
return this;
}

/**
* Keep track of several keys for later removal by a call to {@link #removeSet()}.
* Keep track of several keys for later removal by a call to {@link #()}.
* @param keys
* @return this instance
*/
public MDCHelper addKeys(String... keys) {
public MDCAmbit addKeys(String... keys) {
if(keys == null)
return this;

Expand All @@ -119,15 +119,15 @@ public MDCHelper addKeys(String... keys) {


/**
* Remove tracked keys by calling {@link MDC#remove} on each key.
* Clear tracked keys by calling {@link MDC#remove} on each key.
*
* <p>The set of tracked keys is cleared.</p>
* <p>In addition, the set of tracked keys is cleared.</p>
*
* <p>This method is usually called from within finally statement of a
* try/catch/finally block</p>
*
*/
public void removeSet() {
public void clear() {
for(String key: keySet) {
mdcAdapter.remove(key);
}
Expand Down
18 changes: 9 additions & 9 deletions slf4j-api/src/test/java/org/slf4j/MDCAmbitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class MDCHelperTest {
public class MDCAmbitTest {


BasicMDCAdapter mdcAdapter = new BasicMDCAdapter();
MDCHelper mdch = new MDCHelper(mdcAdapter);
MDCAmbit mdca = new MDCAmbit(mdcAdapter);

String k0 = "k0";
String v0 = "v0";
Expand All @@ -51,14 +51,14 @@ void throwRuntimeException() {
@Test
public void smoke() {
try {
mdch.put(k0, v0);
mdca.put(k0, v0);
assertEquals(v0, mdcAdapter.get(k0));
assertNull(mdcAdapter.get(kUnused));
throwRuntimeException();
} catch (RuntimeException e) {
assertEquals(v0, mdcAdapter.get(k0));
} finally {
mdch.removeSet();
mdca.clear();
}
assertNull(mdcAdapter.get(k0));
}
Expand All @@ -68,7 +68,7 @@ public void empty() {
try {
assertNull(mdcAdapter.get(kUnused));
} finally {
mdch.removeSet();
mdca.clear();
}
assertNull(mdcAdapter.get(k0));
}
Expand All @@ -77,14 +77,14 @@ public void empty() {
public void addKeyTest() {
mdcAdapter.put(k0, v0);
try {
mdch.addKey(k0);
mdca.addKey(k0);
assertEquals(v0, mdcAdapter.get(k0));
assertNull(mdcAdapter.get(kUnused));
throwRuntimeException();
} catch (RuntimeException e) {
assertEquals(v0, mdcAdapter.get(k0));
} finally {
mdch.removeSet();
mdca.clear();
}
assertNull(mdcAdapter.get(k0));
}
Expand All @@ -93,7 +93,7 @@ public void addKeyTest() {
public void combinedPut_addKeyTest() {
mdcAdapter.put(k0, v0);
try {
mdch.addKey(k0).put(k1, v1).put(k2, v2);
mdca.addKey(k0).put(k1, v1).put(k2, v2);
assertEquals(v0, mdcAdapter.get(k0));
assertEquals(v1, mdcAdapter.get(k1));
assertEquals(v2, mdcAdapter.get(k2));
Expand All @@ -104,7 +104,7 @@ public void combinedPut_addKeyTest() {
assertEquals(v1, mdcAdapter.get(k1));
assertEquals(v2, mdcAdapter.get(k2));
} finally {
mdch.removeSet();
mdca.clear();
}
assertNull(mdcAdapter.get(k0));
assertNull(mdcAdapter.get(k1));
Expand Down

0 comments on commit 50b9f5d

Please sign in to comment.