From 3e13d84d984f4a5e3112f5f5231aa70c9e9cdd60 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Wed, 10 May 2023 19:09:41 +0200 Subject: [PATCH] fix a smaller issue with subvalues if a (sub)values have same property names with should be put into the mdc, we get an exception. also properties without the full path would be added, but this means we have no posibility to say we needs the from a give value. --- .../logging/slf4j/Slf4JLogEntryMedia.java | 20 +++++-------- .../logging/slf4j/Slf4JLogEntryMediaTest.java | 28 +++++++++++++------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/media-logging-slf4j/src/main/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMedia.java b/media-logging-slf4j/src/main/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMedia.java index 9dc84b4..a1e22b0 100644 --- a/media-logging-slf4j/src/main/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMedia.java +++ b/media-logging-slf4j/src/main/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMedia.java @@ -127,25 +127,19 @@ public MDCMedia withValue(final String name, final String value) { @Override public MDCMedia withValue(final String name, final Printable value) { - final MDCMedia result; - if (mdcNames.keySet().stream().anyMatch(mdcName -> mdcName.startsWith(String.format("%s.", name)))) { - result = - new MDCMedia( - mdcNames, - new CopyMap.MergeOperator() - .apply(mdcValues, new CopyMap<>(printValue(value).mdcValues())) - ); - } else { - result = this; - } - return result; + return new MDCMedia( + mdcNames, + new CopyMap.MergeOperator() + .apply(mdcValues, new CopyMap<>(printValue(name, value).mdcValues())) + ); } - private MDCMedia printValue(final Printable value) { + private MDCMedia printValue(final String name, final Printable value) { return value.printOn( mdcNames .entrySet() .stream() + .filter(mdcName -> mdcName.getKey().startsWith(name)) .map(entry -> Map.entry(entry.getKey().substring(entry.getKey().indexOf('.') + 1), entry.getValue()) ) diff --git a/media-logging-slf4j/src/test/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMediaTest.java b/media-logging-slf4j/src/test/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMediaTest.java index 8e90ecb..dea2d06 100644 --- a/media-logging-slf4j/src/test/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMediaTest.java +++ b/media-logging-slf4j/src/test/java/io/github/sebastiantoepfer/ddd/media/logging/slf4j/Slf4JLogEntryMediaTest.java @@ -137,16 +137,22 @@ void should_write_values_to_mdc_with_othername() { "username", "user.int", "int", - "unser.long", + "user.long", "long", - "unser.double", + "user.double", "double", - "unser.b1", + "user.b1", "true", - "unser.b2", + "user.b2", "false", - "test", - "other_value" + "user.test", + "other_value", + "user.friend.user_id", + "username_of_friend", + "bigi", + "bigi", + "bigd", + "tina" ) ) .withValue( @@ -164,7 +170,11 @@ public > T printOn(final T media) { .withValue("b2", false) .withValue("col", List.of()) .withValue("bigi", BigInteger.ONE) - .withValue("bigd", BigDecimal.TEN); + .withValue("bigd", BigDecimal.TEN) + .withValue( + "friend", + new TestPrintable(Map.of("name", "Maura", "user_id", "user@gitlab.com")) + ); } } ) @@ -189,7 +199,9 @@ public > T printOn(final T media) { "true", "true", "false", - "false" + "false", + "username_of_friend", + "user@gitlab.com" ) ) )