From 625fd4356a606325626389fa28ee9befbab582ea Mon Sep 17 00:00:00 2001 From: dubasdey Date: Sat, 7 Oct 2023 19:58:17 +0200 Subject: [PATCH] Invalid JSON being generated, missing contextMap Fix for Isue !7 --- pom.xml | 2 +- .../erc/log4j2/layout/JSONLog4j2Layout.java | 2 +- .../log4j2/layout/DummyFilledLogEvent.java | 2 +- .../log4j2/layout/JSONLog4j2LayoutTest.java | 41 +++++++++++++++++-- .../org/erc/log4j2/layout/LoggerConfig.java | 20 +++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 src/test/java/org/erc/log4j2/layout/LoggerConfig.java diff --git a/pom.xml b/pom.xml index 7a50c91..4441512 100644 --- a/pom.xml +++ b/pom.xml @@ -101,7 +101,7 @@ maven-surefire-plugin 2.22.2 - ${argLine} -Xmx1238m + ${argLine} -Xms96m -Xmx512m -Djava.util.logging.config.class=LoggerConfig.class diff --git a/src/main/java/org/erc/log4j2/layout/JSONLog4j2Layout.java b/src/main/java/org/erc/log4j2/layout/JSONLog4j2Layout.java index e719b12..0916c37 100644 --- a/src/main/java/org/erc/log4j2/layout/JSONLog4j2Layout.java +++ b/src/main/java/org/erc/log4j2/layout/JSONLog4j2Layout.java @@ -199,7 +199,7 @@ private void toSerializableEvent(JSONBuilder builder, LogEvent event) { builder.addField( "exception", event.getThrown()); } - if (locationInfo) { + if (locationInfo && event.getSource()!=null) { builder.addField( "file", event.getSource().getFileName()); builder.addField( "line_number", event.getSource().getLineNumber()); builder.addField( "class", event.getSource().getClassName()); diff --git a/src/test/java/org/erc/log4j2/layout/DummyFilledLogEvent.java b/src/test/java/org/erc/log4j2/layout/DummyFilledLogEvent.java index 378537f..df532c1 100644 --- a/src/test/java/org/erc/log4j2/layout/DummyFilledLogEvent.java +++ b/src/test/java/org/erc/log4j2/layout/DummyFilledLogEvent.java @@ -183,7 +183,7 @@ public ReadOnlyStringMap setContextData(ReadOnlyStringMap map) { @Override public Map getContextMap() { // Deprecated skip usage - return null; + return contextMap.getCopy(); } /** diff --git a/src/test/java/org/erc/log4j2/layout/JSONLog4j2LayoutTest.java b/src/test/java/org/erc/log4j2/layout/JSONLog4j2LayoutTest.java index 7de0994..130efe2 100644 --- a/src/test/java/org/erc/log4j2/layout/JSONLog4j2LayoutTest.java +++ b/src/test/java/org/erc/log4j2/layout/JSONLog4j2LayoutTest.java @@ -5,6 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.nio.charset.Charset; +import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.LogEvent; @@ -13,11 +15,13 @@ import org.junit.jupiter.api.Test; /** - * The Class JSONLog4j2LayoutTest. + * The Class JSONLog4j2LayoutTest.contextStack */ @DisplayName("Layout Plugin test") class JSONLog4j2LayoutTest { + private final Logger log = Logger.getLogger("test"); + /** The Constant SHORT_STRING. */ private static final String SHORT_STRING = "Dummy Message Test with tab T\tT before here."; @@ -33,6 +37,7 @@ void testEmptyEvent() { Charset.forName("UTF-8")); LogEvent event = new DummyEmptyLogEvent(); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -45,6 +50,7 @@ void testFilledEvent() { Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(SHORT_STRING); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -58,6 +64,7 @@ void testFilledEventNoContextMap() { LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).setContextData(null); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -72,6 +79,7 @@ void testFilledEventContextMapEmpty() { LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).getContextDataDummy().clear(); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -85,6 +93,7 @@ void testFilledEventContextMapData() { LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).getContextDataDummy().put("A", "B"); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -92,13 +101,27 @@ void testFilledEventContextMapData() { * Test filled event. */ @Test - void testFilledEventContextStack() { + void testFilledEventContextStack1() { JSONLog4j2Layout layout = new JSONLog4j2Layout(false, false, false, "\n", false, null, Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).setContextStack(new DefaultThreadContextStack(true)); - ((DummyFilledLogEvent) event).getContextStack().add("UPS"); + ((DummyFilledLogEvent) event).getContextStack().add("UPS0"); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); + assertNotNull(serializedData,"Serialized data"); + } + + @Test + void testFilledEventContextStack2() { + JSONLog4j2Layout layout = new JSONLog4j2Layout(true, true, true, "\n", true, null, + Charset.forName("UTF-8")); + LogEvent event = new DummyFilledLogEvent(SHORT_STRING); + ((DummyFilledLogEvent) event).setContextStack(new DefaultThreadContextStack(true)); + ((DummyFilledLogEvent) event).getContextStack().add("UPS1"); + ((DummyFilledLogEvent) event).getContextStack().add("UPS2"); + String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -112,6 +135,7 @@ void testFilledEventContextStackEmpty() { LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).setContextStack(ThreadContext.EMPTY_STACK); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -125,6 +149,7 @@ void testFilledEventContextStackNull() { LogEvent event = new DummyFilledLogEvent(SHORT_STRING); ((DummyFilledLogEvent) event).setContextStack(null); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -137,6 +162,7 @@ void testFilledWithLocationEvent() { DummyFilledLogEvent event = new DummyFilledLogEvent(SHORT_STRING); event.setSource(new StackTraceElement("ClassName.class", "MethodElement", "File.java", 42)); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -149,6 +175,7 @@ void testFilledMultiLineEvent() { Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -163,6 +190,7 @@ void testFilledMultiLineWithUserFieldsEvent() { JSONLog4j2Layout layout = new JSONLog4j2Layout(false, false, false, "\n", false, fields,Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -189,6 +217,7 @@ void testThrow() { DummyFilledLogEvent event = new DummyFilledLogEvent(LONG_STRING); event.setThrownDummy(new Throwable("TESTEX")); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -202,6 +231,7 @@ void testNoChartSet() { DummyFilledLogEvent event = new DummyFilledLogEvent(LONG_STRING); event.setThrownDummy(new Throwable("TESTEX")); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -213,6 +243,7 @@ void testFilledMultiLineRemovedEvent() { JSONLog4j2Layout layout = new JSONLog4j2Layout(false, true, false, "\n", false, null, Charset.forName("UTF-8")); LogEvent event = new DummyFilledLogEvent(LONG_STRING); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -220,7 +251,7 @@ void testFilledMultiLineRemovedEvent() { * Test filled multi line event. */ @Test - void testFilledMultiLineWithUserFieldsAndContextMapEvent() { + void testFilledMultiLineWithUserFieldsAndContextMapEvent1() { UserField[] fields = new UserField[2]; fields[0] = new UserField("A1", "B1"); fields[1] = new UserField("A2", "B2"); @@ -230,6 +261,7 @@ void testFilledMultiLineWithUserFieldsAndContextMapEvent() { event.getContextDataDummy().put("CT1", "VALUE"); event.getContextDataDummy().put("CT2", "VALUE"); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } @@ -247,6 +279,7 @@ void testFilledMultiLineWithUserFieldsAndContextMapAsRootEvent() { event.getContextDataDummy().put("CT1", "VALUE"); event.getContextDataDummy().put("CT2", "VALUE"); String serializedData = layout.toSerializable(event); + log.log(Level.INFO,serializedData); assertNotNull(serializedData,"Serialized data"); } } diff --git a/src/test/java/org/erc/log4j2/layout/LoggerConfig.java b/src/test/java/org/erc/log4j2/layout/LoggerConfig.java new file mode 100644 index 0000000..0290873 --- /dev/null +++ b/src/test/java/org/erc/log4j2/layout/LoggerConfig.java @@ -0,0 +1,20 @@ +package org.erc.log4j2.layout; + +import java.util.logging.ConsoleHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +public class LoggerConfig { + public LoggerConfig(){ + System.setProperty("java.util.logging.SimpleFormatter.format","%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n"); + + final ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.FINEST); + consoleHandler.setFormatter(new SimpleFormatter()); + + final Logger app = Logger.getLogger("test"); + app.setLevel(Level.FINEST); + app.addHandler(consoleHandler); + } +}