Skip to content

Commit

Permalink
Merge pull request #8 from dubasdey/7-invalid-json-being-generated-mi…
Browse files Browse the repository at this point in the history
…ssing-contextmap

Invalid JSON being generated, missing contextMap
  • Loading branch information
dubasdey authored Oct 7, 2023
2 parents 7d19889 + 625fd43 commit 05194c6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>${argLine} -Xmx1238m</argLine>
<argLine>${argLine} -Xms96m -Xmx512m -Djava.util.logging.config.class=LoggerConfig.class</argLine>
</configuration>
</plugin>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/erc/log4j2/layout/JSONLog4j2Layout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public ReadOnlyStringMap setContextData(ReadOnlyStringMap map) {
@Override
public Map<String, String> getContextMap() {
// Deprecated skip usage
return null;
return contextMap.getCopy();
}

/**
Expand Down
41 changes: 37 additions & 4 deletions src/test/java/org/erc/log4j2/layout/JSONLog4j2LayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.";

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -85,20 +93,35 @@ 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");
}

/**
* 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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -213,14 +243,15 @@ 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");
}

/**
* 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");
Expand All @@ -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");
}

Expand All @@ -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");
}
}
20 changes: 20 additions & 0 deletions src/test/java/org/erc/log4j2/layout/LoggerConfig.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 05194c6

Please sign in to comment.