diff --git a/pom.xml b/pom.xml
index 1dc589b..522fc52 100644
--- a/pom.xml
+++ b/pom.xml
@@ -66,8 +66,8 @@
maven-compiler-plugin
2.3.2
- 1.5
- 1.5
+ 8
+ 8
@@ -99,25 +99,21 @@
- net.minidev
- json-smart
- 1.1.1
+ org.json
+ json
+ 20240303
log4j
log4j
- 1.2.16
+ 1.2.17
provided
+
- commons-lang
- commons-lang
- 2.6
-
-
- junit
- junit
- 4.8.1
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.10.2
test
diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java
index b29e3fe..47c2f93 100644
--- a/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java
+++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java
@@ -1,9 +1,10 @@
package net.logstash.log4j;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import net.logstash.log4j.data.HostData;
-import net.minidev.json.JSONObject;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.time.FastDateFormat;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
@@ -11,32 +12,23 @@
import java.util.HashMap;
import java.util.Map;
-import java.util.TimeZone;
+import org.json.JSONObject;
public class JSONEventLayoutV0 extends Layout {
- private boolean locationInfo = false;
+ private boolean locationInfo;
- private String tags;
- private boolean ignoreThrowable = false;
+ private final String hostname = new HostData().getHostName();
- private boolean activeIgnoreThrowable = ignoreThrowable;
- private String hostname = new HostData().getHostName();
- private String threadName;
- private long timestamp;
- private String ndc;
- private Map mdc;
- private LocationInfo info;
private HashMap fieldData;
- private HashMap exceptionInformation;
- private JSONObject logstashEvent;
-
- public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
- public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", UTC);
+ public static final DateTimeFormatter ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public static String dateFormat(long timestamp) {
- return ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS.format(timestamp);
+ Instant instant = Instant.ofEpochMilli(timestamp);
+ ZoneId zoneId = ZoneId.of("UTC");
+ LocalDateTime localDate = instant.atZone(zoneId).toLocalDateTime();
+ return localDate.format(ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS);
}
/**
@@ -57,14 +49,14 @@ public JSONEventLayoutV0(boolean locationInfo) {
}
public String format(LoggingEvent loggingEvent) {
- threadName = loggingEvent.getThreadName();
- timestamp = loggingEvent.getTimeStamp();
- fieldData = new HashMap();
- exceptionInformation = new HashMap();
- mdc = loggingEvent.getProperties();
- ndc = loggingEvent.getNDC();
+ String threadName = loggingEvent.getThreadName();
+ long timestamp = loggingEvent.getTimeStamp();
+ fieldData = new HashMap<>();
+ HashMap exceptionInformation = new HashMap<>();
+ Map mdc = loggingEvent.getProperties();
+ String ndc = loggingEvent.getNDC();
- logstashEvent = new JSONObject();
+ JSONObject logstashEvent = new JSONObject();
logstashEvent.put("@source_host", hostname);
logstashEvent.put("@message", loggingEvent.getRenderedMessage());
@@ -79,14 +71,14 @@ public String format(LoggingEvent loggingEvent) {
exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
}
if (throwableInformation.getThrowableStrRep() != null) {
- String stackTrace = StringUtils.join(throwableInformation.getThrowableStrRep(), "\n");
+ String stackTrace = String.join("\n", throwableInformation.getThrowableStrRep());
exceptionInformation.put("stacktrace", stackTrace);
}
addFieldData("exception", exceptionInformation);
}
if (locationInfo) {
- info = loggingEvent.getLocationInformation();
+ LocationInfo info = loggingEvent.getLocationInformation();
addFieldData("file", info.getFileName());
addFieldData("line_number", info.getLineNumber());
addFieldData("class", info.getClassName());
@@ -104,7 +96,7 @@ public String format(LoggingEvent loggingEvent) {
}
public boolean ignoresThrowable() {
- return ignoreThrowable;
+ return false;
}
/**
@@ -126,7 +118,6 @@ public void setLocationInfo(boolean locationInfo) {
}
public void activateOptions() {
- activeIgnoreThrowable = ignoreThrowable;
}
private void addFieldData(String keyname, Object keyval) {
diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
index aaf3228..e26ad7a 100644
--- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
+++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
@@ -1,9 +1,10 @@
package net.logstash.log4j;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import net.logstash.log4j.data.HostData;
-import net.minidev.json.JSONObject;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.time.FastDateFormat;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LocationInfo;
@@ -12,34 +13,28 @@
import java.util.HashMap;
import java.util.Map;
-import java.util.TimeZone;
+import org.json.JSONObject;
public class JSONEventLayoutV1 extends Layout {
- private boolean locationInfo = false;
- private String customUserFields;
+ private boolean locationInfo;
- private boolean ignoreThrowable = false;
+ private String customUserFields;
- private boolean activeIgnoreThrowable = ignoreThrowable;
- private String hostname = new HostData().getHostName();
- private String threadName;
- private long timestamp;
- private String ndc;
- private Map mdc;
- private LocationInfo info;
- private HashMap exceptionInformation;
- private static Integer version = 1;
+ private final String hostname = new HostData().getHostName();
+ private static final Integer version = 1;
private JSONObject logstashEvent;
- public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
- public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", UTC);
+ public static final DateTimeFormatter ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public static final String ADDITIONAL_DATA_PROPERTY = "net.logstash.log4j.JSONEventLayoutV1.UserFields";
public static String dateFormat(long timestamp) {
- return ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS.format(timestamp);
+ Instant instant = Instant.ofEpochMilli(timestamp);
+ ZoneId zoneId = ZoneId.of("UTC");
+ LocalDateTime localDate = instant.atZone(zoneId).toLocalDateTime();
+ return localDate.format(ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS);
}
/**
@@ -60,11 +55,11 @@ public JSONEventLayoutV1(boolean locationInfo) {
}
public String format(LoggingEvent loggingEvent) {
- threadName = loggingEvent.getThreadName();
- timestamp = loggingEvent.getTimeStamp();
- exceptionInformation = new HashMap();
- mdc = loggingEvent.getProperties();
- ndc = loggingEvent.getNDC();
+ String threadName = loggingEvent.getThreadName();
+ long timestamp = loggingEvent.getTimeStamp();
+ HashMap exceptionInformation = new HashMap<>();
+ Map mdc = loggingEvent.getProperties();
+ String ndc = loggingEvent.getNDC();
logstashEvent = new JSONObject();
String whoami = this.getClass().getSimpleName();
@@ -114,14 +109,14 @@ public String format(LoggingEvent loggingEvent) {
exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
}
if (throwableInformation.getThrowableStrRep() != null) {
- String stackTrace = StringUtils.join(throwableInformation.getThrowableStrRep(), "\n");
+ String stackTrace = String.join("\n", throwableInformation.getThrowableStrRep());
exceptionInformation.put("stacktrace", stackTrace);
}
addEventData("exception", exceptionInformation);
}
if (locationInfo) {
- info = loggingEvent.getLocationInformation();
+ LocationInfo info = loggingEvent.getLocationInformation();
addEventData("file", info.getFileName());
addEventData("line_number", info.getLineNumber());
addEventData("class", info.getClassName());
@@ -138,7 +133,7 @@ public String format(LoggingEvent loggingEvent) {
}
public boolean ignoresThrowable() {
- return ignoreThrowable;
+ return false;
}
/**
@@ -163,7 +158,6 @@ public void setLocationInfo(boolean locationInfo) {
public void setUserFields(String userFields) { this.customUserFields = userFields; }
public void activateOptions() {
- activeIgnoreThrowable = ignoreThrowable;
}
private void addUserFields(String data) {
diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java
index 969ccc7..dee1cc5 100644
--- a/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java
+++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java
@@ -1,16 +1,20 @@
package net.logstash.log4j;
-import junit.framework.Assert;
-import net.minidev.json.JSONObject;
-import net.minidev.json.JSONValue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.log4j.MDC;
-import org.junit.After;
-import org.junit.BeforeClass;
-import org.junit.Ignore;
-import org.junit.Test;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
/**
* Created with IntelliJ IDEA.
@@ -29,17 +33,16 @@ public class JSONEventLayoutV0Test {
"@timestamp"
};
- @BeforeClass
+ @BeforeAll
public static void setupTestAppender() {
appender = new MockAppenderV0(new JSONEventLayoutV0());
logger = Logger.getRootLogger();
appender.setThreshold(Level.TRACE);
appender.setName("mockappender");
- appender.activateOptions();
logger.addAppender(appender);
}
- @After
+ @AfterEach
public void clearTestAppender() {
NDC.clear();
appender.clear();
@@ -49,113 +52,104 @@ public void clearTestAppender() {
@Test
public void testJSONEventLayoutIsJSON() {
logger.info("this is an info message");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
+ String message = MockAppenderV0.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
}
@Test
public void testJSONEventLayoutHasKeys() {
logger.info("this is a test message");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
for (String fieldName : logstashFields) {
- Assert.assertTrue("Event does not contain field: " + fieldName, jsonObject.containsKey(fieldName));
+ assertTrue(jsonObject.has(fieldName), "Event does not contain field: " + fieldName);
}
}
@Test
public void testJSONEventLayoutHasFieldLevel() {
logger.fatal("this is a new test message");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertEquals("Log level is wrong", "FATAL", atFields.get("level"));
+ assertEquals("FATAL", atFields.get("level"), "Log level is wrong");
}
@Test
public void testJSONEventLayoutHasNDC() {
- String ndcData = new String("json-layout-test");
+ String ndcData = "json-layout-test";
NDC.push(ndcData);
logger.warn("I should have NDC data in my log");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertEquals("NDC is wrong", ndcData, atFields.get("ndc"));
+ assertEquals(ndcData, atFields.get("ndc"), "NDC is wrong");
}
@Test
public void testJSONEventLayoutHasMDC() {
MDC.put("foo","bar");
logger.warn("I should have MDC data in my log");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
JSONObject mdcData = (JSONObject) atFields.get("mdc");
- Assert.assertEquals("MDC is wrong","bar", mdcData.get("foo"));
+ assertEquals("bar", mdcData.get("foo"), "MDC is wrong");
}
@Test
public void testJSONEventLayoutExceptions() {
- String exceptionMessage = new String("shits on fire, yo");
+ String exceptionMessage = "shits on fire, yo";
logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage));
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
JSONObject exceptionInformation = (JSONObject) atFields.get("exception");
- Assert.assertEquals("Exception class missing", "java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"));
- Assert.assertEquals("Exception exception message", exceptionMessage, exceptionInformation.get("exception_message"));
+ assertEquals("java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"), "Exception class missing");
+ assertEquals(exceptionMessage, exceptionInformation.get("exception_message"), "Exception exception message");
}
@Test
public void testJSONEventLayoutHasClassName() {
logger.warn("warning dawg");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertEquals("Logged class does not match", this.getClass().getCanonicalName().toString(), atFields.get("class"));
+ assertEquals(this.getClass().getCanonicalName(), atFields.get("class"), "Logged class does not match");
}
@Test
public void testJSONEventHasFileName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertNotNull("File value is missing", atFields.get("file"));
+ assertNotNull(atFields.get("file"), "File value is missing");
}
@Test
public void testJSONEventHasLoggerName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertNotNull("LoggerName value is missing", atFields.get("loggerName"));
+ assertNotNull(atFields.get("loggerName"), "LoggerName value is missing");
}
@Test
public void testJSONEventHasThreadName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertNotNull("ThreadName value is missing", atFields.get("threadName"));
+ assertNotNull(atFields.get("threadName"), "ThreadName value is missing");
}
@Test
@@ -166,22 +160,21 @@ public void testJSONEventLayoutNoLocationInfo() {
layout.setLocationInfo(false);
logger.warn("warning dawg");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV0.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
- Assert.assertFalse("atFields contains file value", atFields.containsKey("file"));
- Assert.assertFalse("atFields contains line_number value", atFields.containsKey("line_number"));
- Assert.assertFalse("atFields contains class value", atFields.containsKey("class"));
- Assert.assertFalse("atFields contains method value", atFields.containsKey("method"));
+ assertFalse(atFields.has("line_number"), "atFields contains line_number value");
+ assertFalse(atFields.has("class"), "atFields contains class value");
+ assertFalse(atFields.has("file"), "atFields contains file value");
+ assertFalse(atFields.has("method"), "atFields contains method value");
// Revert the change to the layout to leave it as we found it.
layout.setLocationInfo(prevLocationInfo);
}
@Test
- @Ignore
+ @Disabled
public void measureJSONEventLayoutLocationInfoPerformance() {
JSONEventLayoutV0 layout = (JSONEventLayoutV0) appender.getLayout();
boolean locationInfo = layout.getLocationInfo();
@@ -213,6 +206,16 @@ public void measureJSONEventLayoutLocationInfoPerformance() {
@Test
public void testDateFormat() {
long timestamp = 1364844991207L;
- Assert.assertEquals("format does not produce expected output", "2013-04-01T19:36:31.207Z", JSONEventLayoutV0.dateFormat(timestamp));
+ assertEquals("2013-04-01T19:36:31.207Z", JSONEventLayoutV0.dateFormat(timestamp), "format does not produce expected output");
}
+
+ public boolean isValidJsonStrict(String json) {
+ try {
+ new JSONObject(json);
+ } catch (JSONException e) {
+ return false;
+ }
+ return true;
+ }
+
}
diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
index 96ad821..bfafca5 100644
--- a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
+++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
@@ -1,18 +1,19 @@
package net.logstash.log4j;
-import junit.framework.Assert;
-import net.minidev.json.JSONObject;
-import net.minidev.json.JSONValue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import org.apache.log4j.*;
-import org.apache.log4j.or.ObjectRenderer;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Ignore;
-import org.junit.Test;
import java.util.HashMap;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
/**
* Created with IntelliJ IDEA.
@@ -24,11 +25,9 @@
public class JSONEventLayoutV1Test {
static Logger logger;
static MockAppenderV1 appender;
- static MockAppenderV1 userFieldsAppender;
- static JSONEventLayoutV1 userFieldsLayout;
- static final String userFieldsSingle = new String("field1:value1");
- static final String userFieldsMulti = new String("field2:value2,field3:value3");
- static final String userFieldsSingleProperty = new String("field1:propval1");
+ static final String userFieldsSingle = "field1:value1";
+ static final String userFieldsMulti = "field2:value2,field3:value3";
+ static final String userFieldsSingleProperty = "field1:propval1";
static final String[] logstashFields = new String[]{
"message",
@@ -37,17 +36,16 @@ public class JSONEventLayoutV1Test {
"@version"
};
- @BeforeClass
+ @BeforeAll
public static void setupTestAppender() {
appender = new MockAppenderV1(new JSONEventLayoutV1());
logger = Logger.getRootLogger();
appender.setThreshold(Level.TRACE);
appender.setName("mockappenderv1");
- appender.activateOptions();
logger.addAppender(appender);
}
- @After
+ @AfterEach
public void clearTestAppender() {
NDC.clear();
appender.clear();
@@ -57,20 +55,19 @@ public void clearTestAppender() {
@Test
public void testJSONEventLayoutIsJSON() {
logger.info("this is an info message");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
+ String message = MockAppenderV1.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
}
@Test
public void testJSONEventLayoutHasUserFieldsFromProps() {
System.setProperty(JSONEventLayoutV1.ADDITIONAL_DATA_PROPERTY, userFieldsSingleProperty);
logger.info("this is an info message with user fields");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertTrue("Event does not contain field 'field1'" , jsonObject.containsKey("field1"));
- Assert.assertEquals("Event does not contain value 'value1'", "propval1", jsonObject.get("field1"));
+ String message = MockAppenderV1.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
+ JSONObject jsonObject = new JSONObject(message);
+ assertTrue(jsonObject.has("field1"), "Event does not contain field 'field1'");
+ assertEquals("propval1", jsonObject.get("field1"), "Event does not contain value 'value1'");
System.clearProperty(JSONEventLayoutV1.ADDITIONAL_DATA_PROPERTY);
}
@@ -81,12 +78,11 @@ public void testJSONEventLayoutHasUserFieldsFromConfig() {
layout.setUserFields(userFieldsSingle);
logger.info("this is an info message with user fields");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertTrue("Event does not contain field 'field1'" , jsonObject.containsKey("field1"));
- Assert.assertEquals("Event does not contain value 'value1'", "value1", jsonObject.get("field1"));
+ String message = MockAppenderV1.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
+ JSONObject jsonObject = new JSONObject(message);
+ assertTrue(jsonObject.has("field1"), "Event does not contain field 'field1'");
+ assertEquals("value1", jsonObject.get("field1"), "Event does not contain value 'value1'");
layout.setUserFields(prevUserData);
}
@@ -98,14 +94,13 @@ public void testJSONEventLayoutUserFieldsMulti() {
layout.setUserFields(userFieldsMulti);
logger.info("this is an info message with user fields");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertTrue("Event does not contain field 'field2'" , jsonObject.containsKey("field2"));
- Assert.assertEquals("Event does not contain value 'value2'", "value2", jsonObject.get("field2"));
- Assert.assertTrue("Event does not contain field 'field3'" , jsonObject.containsKey("field3"));
- Assert.assertEquals("Event does not contain value 'value3'", "value3", jsonObject.get("field3"));
+ String message = MockAppenderV1.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
+ JSONObject jsonObject = new JSONObject(message);
+ assertTrue(jsonObject.has("field2"), "Event does not contain field 'field2'");
+ assertEquals("value2", jsonObject.get("field2"), "Event does not contain value 'value2'");
+ assertTrue(jsonObject.has("field3"), "Event does not contain field 'field3'");
+ assertEquals("value3", jsonObject.get("field3"), "Event does not contain value 'value3'");
layout.setUserFields(prevUserData);
}
@@ -121,12 +116,11 @@ public void testJSONEventLayoutUserFieldsPropOverride() {
layout.setUserFields(userFieldsSingle);
logger.info("this is an info message with user fields");
- String message = appender.getMessages()[0];
- Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message));
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertTrue("Event does not contain field 'field1'" , jsonObject.containsKey("field1"));
- Assert.assertEquals("Event does not contain value 'propval1'", "propval1", jsonObject.get("field1"));
+ String message = MockAppenderV1.getMessages()[0];
+ assertTrue(isValidJsonStrict(message), "Event is not valid JSON");
+ JSONObject jsonObject = new JSONObject(message);
+ assertTrue(jsonObject.has("field1"), "Event does not contain field 'field1'");
+ assertEquals("propval1", jsonObject.get("field1"), "Event does not contain value 'propval1'");
layout.setUserFields(prevUserData);
System.clearProperty(JSONEventLayoutV1.ADDITIONAL_DATA_PROPERTY);
@@ -136,103 +130,94 @@ public void testJSONEventLayoutUserFieldsPropOverride() {
@Test
public void testJSONEventLayoutHasKeys() {
logger.info("this is a test message");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
for (String fieldName : logstashFields) {
- Assert.assertTrue("Event does not contain field: " + fieldName, jsonObject.containsKey(fieldName));
+ assertTrue(jsonObject.has(fieldName), "Event does not contain field: " + fieldName);
}
}
@Test
public void testJSONEventLayoutHasNDC() {
- String ndcData = new String("json-layout-test");
+ String ndcData = "json-layout-test";
NDC.push(ndcData);
logger.warn("I should have NDC data in my log");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
- Assert.assertEquals("NDC is wrong", ndcData, jsonObject.get("ndc"));
+ assertEquals(ndcData, jsonObject.get("ndc"), "NDC is wrong");
}
@Test
public void testJSONEventLayoutHasMDC() {
MDC.put("foo", "bar");
logger.warn("I should have MDC data in my log");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject mdc = (JSONObject) jsonObject.get("mdc");
- Assert.assertEquals("MDC is wrong","bar", mdc.get("foo"));
+ assertEquals("bar", mdc.get("foo"), "MDC is wrong");
}
@Test
public void testJSONEventLayoutHasNestedMDC() {
- HashMap nestedMdc = new HashMap();
+ HashMap nestedMdc = new HashMap<>();
nestedMdc.put("bar","baz");
MDC.put("foo",nestedMdc);
logger.warn("I should have nested MDC data in my log");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject mdc = (JSONObject) jsonObject.get("mdc");
JSONObject nested = (JSONObject) mdc.get("foo");
- Assert.assertTrue("Event is missing foo key", mdc.containsKey("foo"));
- Assert.assertEquals("Nested MDC data is wrong", "baz", nested.get("bar"));
+ assertTrue(mdc.has("foo"), "Event is missing foo key");
+ assertEquals("baz", nested.get("bar"), "Nested MDC data is wrong");
}
@Test
public void testJSONEventLayoutExceptions() {
- String exceptionMessage = new String("shits on fire, yo");
+ String exceptionMessage = "shits on fire, yo";
logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage));
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
JSONObject exceptionInformation = (JSONObject) jsonObject.get("exception");
- Assert.assertEquals("Exception class missing", "java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"));
- Assert.assertEquals("Exception exception message", exceptionMessage, exceptionInformation.get("exception_message"));
+ assertEquals("java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"), "Exception class missing");
+ assertEquals(exceptionMessage, exceptionInformation.get("exception_message"), "Exception exception message");
}
@Test
public void testJSONEventLayoutHasClassName() {
logger.warn("warning dawg");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
- Assert.assertEquals("Logged class does not match", this.getClass().getCanonicalName().toString(), jsonObject.get("class"));
+ assertEquals(this.getClass().getCanonicalName(), jsonObject.get("class"), "Logged class does not match");
}
@Test
public void testJSONEventHasFileName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
- Assert.assertNotNull("File value is missing", jsonObject.get("file"));
+ assertNotNull(jsonObject.get("file"), "File value is missing");
}
@Test
public void testJSONEventHasLoggerName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertNotNull("LoggerName value is missing", jsonObject.get("logger_name"));
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
+ assertNotNull(jsonObject.get("logger_name"), "LoggerName value is missing");
}
@Test
public void testJSONEventHasThreadName() {
logger.warn("whoami");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
- Assert.assertNotNull("ThreadName value is missing", jsonObject.get("thread_name"));
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
+ assertNotNull(jsonObject.get("thread_name"), "ThreadName value is missing");
}
@Test
@@ -243,21 +228,20 @@ public void testJSONEventLayoutNoLocationInfo() {
layout.setLocationInfo(false);
logger.warn("warning dawg");
- String message = appender.getMessages()[0];
- Object obj = JSONValue.parse(message);
- JSONObject jsonObject = (JSONObject) obj;
+ String message = MockAppenderV1.getMessages()[0];
+ JSONObject jsonObject = new JSONObject(message);
- Assert.assertFalse("atFields contains file value", jsonObject.containsKey("file"));
- Assert.assertFalse("atFields contains line_number value", jsonObject.containsKey("line_number"));
- Assert.assertFalse("atFields contains class value", jsonObject.containsKey("class"));
- Assert.assertFalse("atFields contains method value", jsonObject.containsKey("method"));
+ assertFalse(jsonObject.has("file"), "atFields contains file value");
+ assertFalse(jsonObject.has("line_number"), "atFields contains line_number value");
+ assertFalse(jsonObject.has("class"), "atFields contains class value");
+ assertFalse(jsonObject.has("method"), "atFields contains method value");
// Revert the change to the layout to leave it as we found it.
layout.setLocationInfo(prevLocationInfo);
}
@Test
- @Ignore
+ @Disabled
public void measureJSONEventLayoutLocationInfoPerformance() {
JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout();
boolean locationInfo = layout.getLocationInfo();
@@ -289,6 +273,15 @@ public void measureJSONEventLayoutLocationInfoPerformance() {
@Test
public void testDateFormat() {
long timestamp = 1364844991207L;
- Assert.assertEquals("format does not produce expected output", "2013-04-01T19:36:31.207Z", JSONEventLayoutV1.dateFormat(timestamp));
+ assertEquals("2013-04-01T19:36:31.207Z", JSONEventLayoutV1.dateFormat(timestamp), "format does not produce expected output");
+ }
+
+ public boolean isValidJsonStrict(String json) {
+ try {
+ new JSONObject(json);
+ } catch (JSONException e) {
+ return false;
+ }
+ return true;
}
}