Skip to content

Commit 2005bc5

Browse files
committed
[log4j-layout-template-json-test] Catch exceptions from readJson and re-throw with serialized event
This should make it a little easier for people running tests to see if (or rather how) they have screwed up the JSON formatting.
1 parent 94f3c35 commit 2005bc5

File tree

1 file changed

+17
-10
lines changed
  • log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json

1 file changed

+17
-10
lines changed

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/TestHelpers.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
*/
1717
package org.apache.logging.log4j.layout.template.json;
1818

19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
import java.util.function.BiConsumer;
22+
import java.util.function.Consumer;
23+
1924
import org.apache.logging.log4j.Level;
2025
import org.apache.logging.log4j.core.Layout;
2126
import org.apache.logging.log4j.core.LogEvent;
@@ -31,11 +36,6 @@
3136
import org.apache.logging.log4j.layout.template.json.util.JsonWriter;
3237
import org.apache.logging.log4j.layout.template.json.util.MapAccessor;
3338

34-
import java.util.LinkedHashMap;
35-
import java.util.Map;
36-
import java.util.function.BiConsumer;
37-
import java.util.function.Consumer;
38-
3939
public final class TestHelpers {
4040

4141
public static final Configuration CONFIGURATION = new DefaultConfiguration();
@@ -74,11 +74,18 @@ public static void usingSerializedLogEventAccessor(
7474
final LogEvent logEvent,
7575
final Consumer<MapAccessor> accessorConsumer) {
7676
final String serializedLogEventJson = layout.toSerializable(logEvent);
77-
@SuppressWarnings("unchecked")
78-
final Map<String, Object> deserializedLogEvent =
79-
(Map<String, Object>) readJson(serializedLogEventJson);
80-
final MapAccessor serializedLogEventAccessor = new MapAccessor(deserializedLogEvent);
81-
accessorConsumer.accept(serializedLogEventAccessor);
77+
78+
try {
79+
@SuppressWarnings("unchecked")
80+
final Map<String, Object> deserializedLogEvent =
81+
(Map<String, Object>) readJson(serializedLogEventJson);
82+
83+
final MapAccessor serializedLogEventAccessor = new MapAccessor(deserializedLogEvent);
84+
accessorConsumer.accept(serializedLogEventAccessor);
85+
} catch (Exception e) {
86+
throw new RuntimeException("failed to deserialize log event (" + e
87+
+ "). Serialized Log Event:\n" + serializedLogEventJson);
88+
}
8289
}
8390

8491
public static Object readJson(final String json) {

0 commit comments

Comments
 (0)