From ab1425c34fe6bc4eceaa4aae37e68e268f175f38 Mon Sep 17 00:00:00 2001 From: Sachin Kainth Date: Tue, 9 Apr 2024 11:36:43 +0100 Subject: [PATCH 1/3] Updated dependencies to more modern versions with no CVEs. --- pom.xml | 20 ++- .../net/logstash/log4j/JSONEventLayoutV0.java | 51 +++---- .../net/logstash/log4j/JSONEventLayoutV1.java | 45 +++--- .../logstash/log4j/JSONEventLayoutV0Test.java | 79 ++++++----- .../logstash/log4j/JSONEventLayoutV1Test.java | 130 +++++++++--------- 5 files changed, 155 insertions(+), 170 deletions(-) diff --git a/pom.xml b/pom.xml index 1dc589b..43b3efa 100644 --- a/pom.xml +++ b/pom.xml @@ -66,8 +66,8 @@ maven-compiler-plugin 2.3.2 - 1.5 - 1.5 + 8 + 8 @@ -101,23 +101,19 @@ net.minidev json-smart - 1.1.1 + 2.5.1 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..c3c2421 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java @@ -1,9 +1,11 @@ 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 +13,22 @@ import java.util.HashMap; import java.util.Map; -import java.util.TimeZone; 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..f3e76cb 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java @@ -1,9 +1,11 @@ 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; @@ -16,30 +18,26 @@ public class JSONEventLayoutV1 extends Layout { - private boolean locationInfo = false; + private boolean locationInfo; + private String customUserFields; - private boolean ignoreThrowable = false; + private final boolean ignoreThrowable = false; - 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 +58,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 +112,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()); @@ -163,7 +161,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..3f18182 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 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 net.minidev.json.JSONObject; import net.minidev.json.JSONValue; 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.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,113 @@ 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(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); } @Test public void testJSONEventLayoutHasKeys() { logger.info("this is a test message"); - String message = appender.getMessages()[0]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; for (String fieldName : logstashFields) { - Assert.assertTrue("Event does not contain field: " + fieldName, jsonObject.containsKey(fieldName)); + assertTrue(jsonObject.containsKey(fieldName), "Event does not contain field: " + fieldName); } } @Test public void testJSONEventLayoutHasFieldLevel() { logger.fatal("this is a new test message"); - String message = appender.getMessages()[0]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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 +169,22 @@ public void testJSONEventLayoutNoLocationInfo() { layout.setLocationInfo(false); logger.warn("warning dawg"); - String message = appender.getMessages()[0]; + String message = MockAppenderV0.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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.containsKey("line_number"), "atFields contains line_number value"); + assertFalse(atFields.containsKey("class"), "atFields contains class value"); + assertFalse(atFields.containsKey("file"), "atFields contains file value"); + assertFalse(atFields.containsKey("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 +216,6 @@ 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"); } } diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java index 96ad821..ba7efcc 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 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 net.minidev.json.JSONObject; import net.minidev.json.JSONValue; 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.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,20 @@ 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(JSONValue.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)); + String message = MockAppenderV1.getMessages()[0]; + assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); 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")); + assertTrue(jsonObject.containsKey("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 +79,12 @@ 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)); + String message = MockAppenderV1.getMessages()[0]; + assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); 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")); + assertTrue(jsonObject.containsKey("field1"), "Event does not contain field 'field1'"); + assertEquals("value1", jsonObject.get("field1"), "Event does not contain value 'value1'"); layout.setUserFields(prevUserData); } @@ -98,14 +96,14 @@ 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)); + String message = MockAppenderV1.getMessages()[0]; + assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); 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")); + assertTrue(jsonObject.containsKey("field2"), "Event does not contain field 'field2'"); + assertEquals("value2", jsonObject.get("field2"), "Event does not contain value 'value2'"); + assertTrue(jsonObject.containsKey("field3"), "Event does not contain field 'field3'"); + assertEquals("value3", jsonObject.get("field3"), "Event does not contain value 'value3'"); layout.setUserFields(prevUserData); } @@ -121,12 +119,12 @@ 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)); + String message = MockAppenderV1.getMessages()[0]; + assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); 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")); + assertTrue(jsonObject.containsKey("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 +134,103 @@ public void testJSONEventLayoutUserFieldsPropOverride() { @Test public void testJSONEventLayoutHasKeys() { logger.info("this is a test message"); - String message = appender.getMessages()[0]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; for (String fieldName : logstashFields) { - Assert.assertTrue("Event does not contain field: " + fieldName, jsonObject.containsKey(fieldName)); + assertTrue(jsonObject.containsKey(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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - 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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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.containsKey("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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; 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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - 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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - 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]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - Assert.assertNotNull("LoggerName value is missing", jsonObject.get("logger_name")); + assertNotNull(jsonObject.get("logger_name"), "LoggerName value is missing"); } @Test public void testJSONEventHasThreadName() { logger.warn("whoami"); - String message = appender.getMessages()[0]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - Assert.assertNotNull("ThreadName value is missing", jsonObject.get("thread_name")); + assertNotNull(jsonObject.get("thread_name"), "ThreadName value is missing"); } @Test @@ -243,21 +241,21 @@ public void testJSONEventLayoutNoLocationInfo() { layout.setLocationInfo(false); logger.warn("warning dawg"); - String message = appender.getMessages()[0]; + String message = MockAppenderV1.getMessages()[0]; Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - 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.containsKey("file"), "atFields contains file value"); + assertFalse(jsonObject.containsKey("line_number"), "atFields contains line_number value"); + assertFalse(jsonObject.containsKey("class"), "atFields contains class value"); + assertFalse(jsonObject.containsKey("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 +287,6 @@ 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"); } } From 6c8fcf693abdcbd307c74b3097e6953fd8539d7f Mon Sep 17 00:00:00 2001 From: Sachin Kainth Date: Tue, 9 Apr 2024 13:52:58 +0100 Subject: [PATCH 2/3] Minor change. --- src/main/java/net/logstash/log4j/JSONEventLayoutV1.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java index f3e76cb..bf1cdb2 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java @@ -14,7 +14,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.TimeZone; public class JSONEventLayoutV1 extends Layout { @@ -22,8 +21,6 @@ public class JSONEventLayoutV1 extends Layout { private String customUserFields; - private final boolean ignoreThrowable = false; - private final String hostname = new HostData().getHostName(); private static final Integer version = 1; @@ -136,7 +133,7 @@ public String format(LoggingEvent loggingEvent) { } public boolean ignoresThrowable() { - return ignoreThrowable; + return false; } /** From 55b2cbb5423908e8de3248ae37f53c3336dc436b Mon Sep 17 00:00:00 2001 From: Sachin Kainth Date: Wed, 10 Apr 2024 09:43:10 +0100 Subject: [PATCH 3/3] Upgraded Minidev Json to Json.org --- pom.xml | 6 +- .../net/logstash/log4j/JSONEventLayoutV0.java | 2 +- .../net/logstash/log4j/JSONEventLayoutV1.java | 2 +- .../logstash/log4j/JSONEventLayoutV0Test.java | 56 ++++++------ .../logstash/log4j/JSONEventLayoutV1Test.java | 87 +++++++++---------- 5 files changed, 74 insertions(+), 79 deletions(-) diff --git a/pom.xml b/pom.xml index 43b3efa..522fc52 100644 --- a/pom.xml +++ b/pom.xml @@ -99,9 +99,9 @@ - net.minidev - json-smart - 2.5.1 + org.json + json + 20240303 log4j diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java index c3c2421..47c2f93 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV0.java @@ -5,7 +5,6 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import net.logstash.log4j.data.HostData; -import net.minidev.json.JSONObject; import org.apache.log4j.Layout; import org.apache.log4j.spi.LocationInfo; import org.apache.log4j.spi.LoggingEvent; @@ -13,6 +12,7 @@ import java.util.HashMap; import java.util.Map; +import org.json.JSONObject; public class JSONEventLayoutV0 extends Layout { diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java index bf1cdb2..e26ad7a 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java @@ -5,7 +5,6 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import net.logstash.log4j.data.HostData; -import net.minidev.json.JSONObject; import org.apache.log4j.Layout; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.spi.LocationInfo; @@ -14,6 +13,7 @@ import java.util.HashMap; import java.util.Map; +import org.json.JSONObject; public class JSONEventLayoutV1 extends Layout { diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java index 3f18182..dee1cc5 100644 --- a/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java +++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java @@ -5,12 +5,12 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import net.minidev.json.JSONObject; -import net.minidev.json.JSONValue; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.NDC; import org.apache.log4j.MDC; +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; @@ -53,18 +53,17 @@ public void clearTestAppender() { public void testJSONEventLayoutIsJSON() { logger.info("this is an info message"); String message = MockAppenderV0.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); + assertTrue(isValidJsonStrict(message), "Event is not valid JSON"); } @Test public void testJSONEventLayoutHasKeys() { logger.info("this is a test message"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); for (String fieldName : logstashFields) { - assertTrue(jsonObject.containsKey(fieldName), "Event does not contain field: " + fieldName); + assertTrue(jsonObject.has(fieldName), "Event does not contain field: " + fieldName); } } @@ -72,8 +71,7 @@ public void testJSONEventLayoutHasKeys() { public void testJSONEventLayoutHasFieldLevel() { logger.fatal("this is a new test message"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertEquals("FATAL", atFields.get("level"), "Log level is wrong"); @@ -85,8 +83,7 @@ public void testJSONEventLayoutHasNDC() { NDC.push(ndcData); logger.warn("I should have NDC data in my log"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertEquals(ndcData, atFields.get("ndc"), "NDC is wrong"); @@ -97,8 +94,7 @@ public void testJSONEventLayoutHasMDC() { MDC.put("foo","bar"); logger.warn("I should have MDC data in my log"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); JSONObject mdcData = (JSONObject) atFields.get("mdc"); @@ -110,8 +106,7 @@ public void testJSONEventLayoutExceptions() { String exceptionMessage = "shits on fire, yo"; logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage)); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); JSONObject exceptionInformation = (JSONObject) atFields.get("exception"); @@ -123,8 +118,7 @@ public void testJSONEventLayoutExceptions() { public void testJSONEventLayoutHasClassName() { logger.warn("warning dawg"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertEquals(this.getClass().getCanonicalName(), atFields.get("class"), "Logged class does not match"); @@ -134,8 +128,7 @@ public void testJSONEventLayoutHasClassName() { public void testJSONEventHasFileName() { logger.warn("whoami"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertNotNull(atFields.get("file"), "File value is missing"); @@ -145,8 +138,7 @@ public void testJSONEventHasFileName() { public void testJSONEventHasLoggerName() { logger.warn("whoami"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertNotNull(atFields.get("loggerName"), "LoggerName value is missing"); } @@ -155,8 +147,7 @@ public void testJSONEventHasLoggerName() { public void testJSONEventHasThreadName() { logger.warn("whoami"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); assertNotNull(atFields.get("threadName"), "ThreadName value is missing"); } @@ -170,14 +161,13 @@ public void testJSONEventLayoutNoLocationInfo() { logger.warn("warning dawg"); String message = MockAppenderV0.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject atFields = (JSONObject) jsonObject.get("@fields"); - assertFalse(atFields.containsKey("line_number"), "atFields contains line_number value"); - assertFalse(atFields.containsKey("class"), "atFields contains class value"); - assertFalse(atFields.containsKey("file"), "atFields contains file value"); - assertFalse(atFields.containsKey("method"), "atFields contains method value"); + 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); @@ -218,4 +208,14 @@ public void testDateFormat() { long timestamp = 1364844991207L; 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 ba7efcc..bfafca5 100644 --- a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java +++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java @@ -5,11 +5,11 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import net.minidev.json.JSONObject; -import net.minidev.json.JSONValue; import org.apache.log4j.*; 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; @@ -56,7 +56,7 @@ public void clearTestAppender() { public void testJSONEventLayoutIsJSON() { logger.info("this is an info message"); String message = MockAppenderV1.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); + assertTrue(isValidJsonStrict(message), "Event is not valid JSON"); } @Test @@ -64,10 +64,9 @@ public void testJSONEventLayoutHasUserFieldsFromProps() { System.setProperty(JSONEventLayoutV1.ADDITIONAL_DATA_PROPERTY, userFieldsSingleProperty); logger.info("this is an info message with user fields"); String message = MockAppenderV1.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; - assertTrue(jsonObject.containsKey("field1"), "Event does not contain field 'field1'"); + 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); } @@ -80,10 +79,9 @@ public void testJSONEventLayoutHasUserFieldsFromConfig() { logger.info("this is an info message with user fields"); String message = MockAppenderV1.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; - assertTrue(jsonObject.containsKey("field1"), "Event does not contain field 'field1'"); + 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); @@ -97,12 +95,11 @@ public void testJSONEventLayoutUserFieldsMulti() { logger.info("this is an info message with user fields"); String message = MockAppenderV1.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; - assertTrue(jsonObject.containsKey("field2"), "Event does not contain field 'field2'"); + 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.containsKey("field3"), "Event does not contain field 'field3'"); + assertTrue(jsonObject.has("field3"), "Event does not contain field 'field3'"); assertEquals("value3", jsonObject.get("field3"), "Event does not contain value 'value3'"); layout.setUserFields(prevUserData); @@ -120,10 +117,9 @@ public void testJSONEventLayoutUserFieldsPropOverride() { logger.info("this is an info message with user fields"); String message = MockAppenderV1.getMessages()[0]; - assertTrue(JSONValue.isValidJsonStrict(message), "Event is not valid JSON"); - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; - assertTrue(jsonObject.containsKey("field1"), "Event does not contain field 'field1'"); + 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); @@ -135,10 +131,9 @@ public void testJSONEventLayoutUserFieldsPropOverride() { public void testJSONEventLayoutHasKeys() { logger.info("this is a test message"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); for (String fieldName : logstashFields) { - assertTrue(jsonObject.containsKey(fieldName), "Event does not contain field: " + fieldName); + assertTrue(jsonObject.has(fieldName), "Event does not contain field: " + fieldName); } } @@ -148,8 +143,7 @@ public void testJSONEventLayoutHasNDC() { NDC.push(ndcData); logger.warn("I should have NDC data in my log"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); assertEquals(ndcData, jsonObject.get("ndc"), "NDC is wrong"); } @@ -159,8 +153,7 @@ public void testJSONEventLayoutHasMDC() { MDC.put("foo", "bar"); logger.warn("I should have MDC data in my log"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject mdc = (JSONObject) jsonObject.get("mdc"); assertEquals("bar", mdc.get("foo"), "MDC is wrong"); @@ -173,12 +166,11 @@ public void testJSONEventLayoutHasNestedMDC() { MDC.put("foo",nestedMdc); logger.warn("I should have nested MDC data in my log"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject mdc = (JSONObject) jsonObject.get("mdc"); JSONObject nested = (JSONObject) mdc.get("foo"); - assertTrue(mdc.containsKey("foo"), "Event is missing foo key"); + assertTrue(mdc.has("foo"), "Event is missing foo key"); assertEquals("baz", nested.get("bar"), "Nested MDC data is wrong"); } @@ -187,8 +179,7 @@ public void testJSONEventLayoutExceptions() { String exceptionMessage = "shits on fire, yo"; logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage)); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); JSONObject exceptionInformation = (JSONObject) jsonObject.get("exception"); assertEquals("java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"), "Exception class missing"); @@ -199,8 +190,7 @@ public void testJSONEventLayoutExceptions() { public void testJSONEventLayoutHasClassName() { logger.warn("warning dawg"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); assertEquals(this.getClass().getCanonicalName(), jsonObject.get("class"), "Logged class does not match"); } @@ -209,8 +199,7 @@ public void testJSONEventLayoutHasClassName() { public void testJSONEventHasFileName() { logger.warn("whoami"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); assertNotNull(jsonObject.get("file"), "File value is missing"); } @@ -219,8 +208,7 @@ public void testJSONEventHasFileName() { public void testJSONEventHasLoggerName() { logger.warn("whoami"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); assertNotNull(jsonObject.get("logger_name"), "LoggerName value is missing"); } @@ -228,8 +216,7 @@ public void testJSONEventHasLoggerName() { public void testJSONEventHasThreadName() { logger.warn("whoami"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); assertNotNull(jsonObject.get("thread_name"), "ThreadName value is missing"); } @@ -242,13 +229,12 @@ public void testJSONEventLayoutNoLocationInfo() { logger.warn("warning dawg"); String message = MockAppenderV1.getMessages()[0]; - Object obj = JSONValue.parse(message); - JSONObject jsonObject = (JSONObject) obj; + JSONObject jsonObject = new JSONObject(message); - assertFalse(jsonObject.containsKey("file"), "atFields contains file value"); - assertFalse(jsonObject.containsKey("line_number"), "atFields contains line_number value"); - assertFalse(jsonObject.containsKey("class"), "atFields contains class value"); - assertFalse(jsonObject.containsKey("method"), "atFields contains method value"); + 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); @@ -289,4 +275,13 @@ public void testDateFormat() { long timestamp = 1364844991207L; 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; + } }