Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement putAll and putAllCloseable #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 75 additions & 1 deletion slf4j-api/src/main/java/org/slf4j/MDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import java.io.Closeable;
import java.util.Deque;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.NOPMDCAdapter;
Expand Down Expand Up @@ -74,13 +76,25 @@ public class MDC {
*/
public static class MDCCloseable implements Closeable {
private final String key;
private final Set<String> keys;

private MDCCloseable(String key) {
this.key = key;
this.keys = null;
}

private MDCCloseable(Set<String> keys) {
this.key = null;
this.keys = new HashSet<>(keys);
}

public void close() {
MDC.remove(this.key);
if (this.key != null) {
MDC.remove(this.key);
}
if (this.keys != null) {
this.keys.forEach(MDC::remove);
}
}
}

Expand Down Expand Up @@ -123,6 +137,32 @@ public static void put(String key, String val) throws IllegalArgumentException {
mdcAdapter.put(key, val);
}

/**
* Put a diagnostic context map (the <code>entries</code> parameter) as identified with each
* <code>key</code> into the current thread's diagnostic context map. The
* <code>entries</code> parameter and its <code>keys</code> cannot be null. The
* <code>value</code> of each entry can be null only if the underlying implementation
* supports it.
*
* <p>
* This method delegates all work to the MDC of the underlying logging system.
*
* @param entries entries to put in the map
*
* @throws IllegalArgumentException
* in case the "entries" parameter is null
* @since 2.0.0
*/
public static void putAll(Map<String, String> entries) throws IllegalArgumentException {
if (entries == null) {
throw new IllegalArgumentException("entries parameter cannot be null");
}
if (mdcAdapter == null) {
throw new IllegalStateException("MDCAdapter cannot be null. See also " + NULL_MDCA_URL);
}
entries.forEach((k,v) -> mdcAdapter.put(k, v));
}

/**
* Put a diagnostic context value (the <code>val</code> parameter) as identified with the
* <code>key</code> parameter into the current thread's diagnostic context map. The
Expand Down Expand Up @@ -156,6 +196,40 @@ public static MDCCloseable putCloseable(String key, String val) throws IllegalAr
return new MDCCloseable(key);
}

/**
* Put a diagnostic context map (the <code>entries</code> parameter) as identified with each
* <code>key</code> into the current thread's diagnostic context map. The
* <code>entries</code> parameter and its <code>keys</code> cannot be null. The
* <code>value</code> of each entry can be null only if the underlying implementation
* supports it.
*
* <p>
* This method delegates all work to the MDC of the underlying logging system.
* <p>
* This method returns a <code>Closeable</code> object which can remove all <code>keys</code>
* that are part of the <code>entries</code> when <code>close</code> is called.
*
* <p>
* Useful with Java 9 for example :
* <code>
* try(MDC.MDCCloseable closeable = MDC.putAllCloseable(Map.of(key1, value, key2, value2)) {
* ....
* }
* </code>
*
* @param entries entries to put in the map
* @return a <code>Closeable</code> who can remove all originally supplied <code>keys</code>
* when <code>close</code> is called.
*
* @throws IllegalArgumentException
* in case the "entries" parameter is null
* @since 2.0.0
*/
public static MDCCloseable putAllCloseable(Map<String, String> entries) throws IllegalArgumentException {
Copy link
Contributor

@ascopes ascopes Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would an override like java.util.Map.of(String, String, ...) be useful here?

In many of the places in applications where I have wanted to use this sort of functionality, I have usually wanted to add several entries at once. In this case it would read something like

try (MDC.putAllCloseable(
    "X-Request-ID", request.getHeader("X-Request-ID"),
    "User-Agent", request.getHeader("User-Agent"),
    "From", request.getHeader("From")
)) {
    processRequest();
}

versus having to declare this separately.

If not, I wonder if having the ability to chain additional put operations onto the closeable may be useful, along with a functional method to run the closeable in a closure directly.

MDC.putCloseable("foo", "bar")
   .put("baz", "bork")
   .put("qux", "quxx")
   .run(() -> {
       processRequest();
   });

where run has a signature of something like

@FunctionalInterface
public interface ThrowingRunnable<T extends Throwable> {
  void run() throws T;
}

@FunctionalInterface
public interface ThrowingSupplier<R, T extends Throwable> {
  R get() throws T;
}

public void <T extends Throwable> run(ThrowingRunnable<T> runnable) throws T {
  try (this) {
    runnable.run();
  }
}

public void <R, T extends Throwable> run(ThrowingSupplier<R, T> supplier) throws T {
  try (this) {
    return supplier.get();
  }
}

Probably out of scope for this PR, but wondering if it may compliment this functionality in the future perhaps?

MDC.putAll(entries);
return new MDCCloseable(entries.keySet());
}

/**
* Get the diagnostic context identified by the <code>key</code> parameter. The
* <code>key</code> parameter cannot be null.
Expand Down
123 changes: 123 additions & 0 deletions slf4j-jdk14/src/test/java/org/slf4j/jul/InvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.junit.Test;
import org.slf4j.*;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -170,6 +172,127 @@ public void testMDC() {
}
}

@Test
public void testMDCContextMapValues() {
Map<String, String> map = new HashMap<>();
map.put("ka", "va");
map.put("kb", "vb");

MDC.put("k", "v");
assertEquals("v", MDC.get("k"));
MDC.setContextMap(map);
assertNull(MDC.get("k"));
assertEquals("va", MDC.get("ka"));
assertEquals("vb", MDC.get("kb"));
}


@Test
public void testMDCPutAll() {
Map<String, String> values = new HashMap<>();
values.put("k1", "v1");
values.put("k2", "v2");

MDC.put("k", "v");
MDC.putAll(values);

assertNotNull(MDC.get("k"));
assertNotNull(MDC.get("k1"));
assertNotNull(MDC.get("k2"));
MDC.remove("k1");
MDC.remove("k2");
assertNotNull(MDC.get("k"));
assertNull(MDC.get("k1"));
assertNull(MDC.get("k2"));
MDC.clear();
}

@Test
public void testMDCCloseable() {
MDC.put("pre", "v");
try(MDC.MDCCloseable mdcCloseable = MDC.putCloseable("try-with", "v")) {
assertNotNull(MDC.get("pre"));
assertNotNull(MDC.get("try-with"));
assertNull(MDC.get("post"));
}
MDC.put("post", "v");
assertNotNull(MDC.get("pre"));
assertNull(MDC.get("try-with"));
assertNotNull(MDC.get("post"));
MDC.clear();
}

@Test
public void testMDCCloseableOverwrites() {
MDC.put("pre", "v");
try(MDC.MDCCloseable mdcCloseable = MDC.putCloseable("pre", "v2")) {
assertNotNull(MDC.get("pre"));
assertEquals("v2", MDC.get("pre"));
assertNull(MDC.get("post"));
}
MDC.put("post", "v");
assertNull(MDC.get("pre"));
assertNotNull(MDC.get("post"));
MDC.clear();
}

@Test
public void testMDCCloseablePutAll() {
Map<String, String> values = new HashMap<>();
values.put("try-with", "v");

MDC.put("pre", "v");
try(MDC.MDCCloseable mdcCloseable = MDC.putAllCloseable(values)) {
assertNotNull(MDC.get("pre"));
assertNotNull(MDC.get("try-with"));
assertNull(MDC.get("post"));
}
MDC.put("post", "v");
assertNotNull(MDC.get("pre"));
assertNull(MDC.get("try-with"));
assertNotNull(MDC.get("post"));
MDC.clear();
}

@Test
public void testMDCCloseablePutAllOverwrites() {
Map<String, String> values = new HashMap<>();
values.put("pre", "v2");
values.put("try-with", "v");

MDC.put("pre", "v");
try(MDC.MDCCloseable mdcCloseable = MDC.putAllCloseable(values)) {
assertNotNull(MDC.get("pre"));
assertEquals("v2", MDC.get("pre"));
assertNotNull(MDC.get("try-with"));
assertNull(MDC.get("post"));
}
MDC.put("post", "v");
assertNull(MDC.get("pre"));
assertNull(MDC.get("try-with"));
assertNotNull(MDC.get("post"));
MDC.clear();
}

@Test
public void testMDCCloseablePutAllImmutable() {
Map<String, String> values = new HashMap<>();
values.put("try-with", "v");

MDC.put("pre", "v");
try(MDC.MDCCloseable mdcCloseable = MDC.putAllCloseable(values)) {
values.remove("try-with");
assertNotNull(MDC.get("pre"));
assertNotNull(MDC.get("try-with"));
assertNull(MDC.get("post"));
}
MDC.put("post", "v");
assertNotNull(MDC.get("pre"));
assertNull(MDC.get("try-with"));
assertNotNull(MDC.get("post"));
MDC.clear();
}

private void assertLogMessage(String expected, int index) {
LogRecord logRecord = listHandler.recordList.get(index);
Assert.assertNotNull(logRecord);
Expand Down
32 changes: 32 additions & 0 deletions slf4j-nop/src/test/java/org/slf4j/nop/InvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* Test whether invoking the SLF4J API causes problems or not.
*
Expand Down Expand Up @@ -115,6 +118,21 @@ public void testMDC() {
MDC.clear();
}

@Test
public void testMDCPutAll() {
Map<String, String> values = new HashMap<>();
values.put("key1", "value1");
values.put("key2", "value2");
MDC.putAll(values);
assertNull(MDC.get("key1"));
assertNull(MDC.get("key2"));
MDC.remove("key1");
MDC.remove("key2");
assertNull(MDC.get("key1"));
assertNull(MDC.get("key2"));
MDC.clear();
}

@Test
public void testMDCCloseable() {
MDC.MDCCloseable closeable = MDC.putCloseable("k", "v");
Expand All @@ -123,4 +141,18 @@ public void testMDCCloseable() {
assertNull(MDC.get("k"));
MDC.clear();
}

@Test
public void testMDCCloseablePutAll() {
Map<String, String> values = new HashMap<>();
values.put("key1", "value1");
values.put("key2", "value2");
MDC.MDCCloseable closeable = MDC.putAllCloseable(values);
assertNull(MDC.get("key1"));
assertNull(MDC.get("key2"));
closeable.close();
assertNull(MDC.get("key1"));
assertNull(MDC.get("key2"));
MDC.clear();
}
}