|
| 1 | +package net.logstash.log4j; |
| 2 | + |
| 3 | +import junit.framework.Assert; |
| 4 | +import net.minidev.json.JSONObject; |
| 5 | +import net.minidev.json.JSONValue; |
| 6 | +import org.apache.log4j.Level; |
| 7 | +import org.apache.log4j.Logger; |
| 8 | +import org.apache.log4j.NDC; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.AfterClass; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Ignore; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +/** |
| 17 | + * Created with IntelliJ IDEA. |
| 18 | + * User: jvincent |
| 19 | + * Date: 12/5/12 |
| 20 | + * Time: 12:07 AM |
| 21 | + * To change this template use File | Settings | File Templates. |
| 22 | + */ |
| 23 | +public class JSONEventV1LayoutTest { |
| 24 | + static Logger logger; |
| 25 | + static MockAppenderV1 appender; |
| 26 | + static final String[] logstashFields = new String[]{ |
| 27 | + "message", |
| 28 | + "source_host", |
| 29 | + "@timestamp", |
| 30 | + "@version" |
| 31 | + }; |
| 32 | + |
| 33 | + @BeforeClass |
| 34 | + public static void setupTestAppender() { |
| 35 | + appender = new MockAppenderV1(new JSONEventLayoutV1()); |
| 36 | + logger = Logger.getRootLogger(); |
| 37 | + appender.setThreshold(Level.TRACE); |
| 38 | + appender.setName("mockappenderv1"); |
| 39 | + appender.activateOptions(); |
| 40 | + logger.addAppender(appender); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void clearTestAppender() { |
| 45 | + NDC.clear(); |
| 46 | + appender.clear(); |
| 47 | + appender.close(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testJSONEventLayoutIsJSON() { |
| 52 | + logger.info("this is an info message"); |
| 53 | + String message = appender.getMessages()[0]; |
| 54 | + Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message)); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testJSONEventLayoutHasKeys() { |
| 59 | + logger.info("this is a test message"); |
| 60 | + String message = appender.getMessages()[0]; |
| 61 | + Object obj = JSONValue.parse(message); |
| 62 | + JSONObject jsonObject = (JSONObject) obj; |
| 63 | + for (String fieldName : logstashFields) { |
| 64 | + Assert.assertTrue("Event does not contain field: " + fieldName, jsonObject.containsKey(fieldName)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testJSONEventLayoutHasNDC() { |
| 70 | + String ndcData = new String("json-layout-test"); |
| 71 | + NDC.push(ndcData); |
| 72 | + logger.warn("I should have NDC data in my log"); |
| 73 | + String message = appender.getMessages()[0]; |
| 74 | + Object obj = JSONValue.parse(message); |
| 75 | + JSONObject jsonObject = (JSONObject) obj; |
| 76 | + |
| 77 | + Assert.assertEquals("NDC is wrong", ndcData, jsonObject.get("ndc")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testJSONEventLayoutExceptions() { |
| 82 | + String exceptionMessage = new String("shits on fire, yo"); |
| 83 | + logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage)); |
| 84 | + String message = appender.getMessages()[0]; |
| 85 | + Object obj = JSONValue.parse(message); |
| 86 | + JSONObject jsonObject = (JSONObject) obj; |
| 87 | + JSONObject exceptionInformation = (JSONObject) jsonObject.get("exception"); |
| 88 | + |
| 89 | + Assert.assertEquals("Exception class missing", "java.lang.IllegalArgumentException", exceptionInformation.get("exception_class")); |
| 90 | + Assert.assertEquals("Exception exception message", exceptionMessage, exceptionInformation.get("exception_message")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testJSONEventLayoutHasClassName() { |
| 95 | + logger.warn("warning dawg"); |
| 96 | + String message = appender.getMessages()[0]; |
| 97 | + Object obj = JSONValue.parse(message); |
| 98 | + JSONObject jsonObject = (JSONObject) obj; |
| 99 | + |
| 100 | + Assert.assertEquals("Logged class does not match", this.getClass().getCanonicalName().toString(), jsonObject.get("class")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testJSONEventHasFileName() { |
| 105 | + logger.warn("whoami"); |
| 106 | + String message = appender.getMessages()[0]; |
| 107 | + Object obj = JSONValue.parse(message); |
| 108 | + JSONObject jsonObject = (JSONObject) obj; |
| 109 | + |
| 110 | + Assert.assertNotNull("File value is missing", jsonObject.get("file")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testJSONEventHasLoggerName() { |
| 115 | + logger.warn("whoami"); |
| 116 | + String message = appender.getMessages()[0]; |
| 117 | + Object obj = JSONValue.parse(message); |
| 118 | + JSONObject jsonObject = (JSONObject) obj; |
| 119 | + Assert.assertNotNull("LoggerName value is missing", jsonObject.get("logger_name")); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testJSONEventHasThreadName() { |
| 124 | + logger.warn("whoami"); |
| 125 | + String message = appender.getMessages()[0]; |
| 126 | + Object obj = JSONValue.parse(message); |
| 127 | + JSONObject jsonObject = (JSONObject) obj; |
| 128 | + Assert.assertNotNull("ThreadName value is missing", jsonObject.get("thread_name")); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testJSONEventLayoutNoLocationInfo() { |
| 133 | + JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout(); |
| 134 | + boolean prevLocationInfo = layout.getLocationInfo(); |
| 135 | + |
| 136 | + layout.setLocationInfo(false); |
| 137 | + |
| 138 | + logger.warn("warning dawg"); |
| 139 | + String message = appender.getMessages()[0]; |
| 140 | + Object obj = JSONValue.parse(message); |
| 141 | + JSONObject jsonObject = (JSONObject) obj; |
| 142 | + |
| 143 | + Assert.assertFalse("atFields contains file value", jsonObject.containsKey("file")); |
| 144 | + Assert.assertFalse("atFields contains line_number value", jsonObject.containsKey("line_number")); |
| 145 | + Assert.assertFalse("atFields contains class value", jsonObject.containsKey("class")); |
| 146 | + Assert.assertFalse("atFields contains method value", jsonObject.containsKey("method")); |
| 147 | + |
| 148 | + // Revert the change to the layout to leave it as we found it. |
| 149 | + layout.setLocationInfo(prevLocationInfo); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @Ignore |
| 154 | + public void measureJSONEventLayoutLocationInfoPerformance() { |
| 155 | + JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout(); |
| 156 | + boolean locationInfo = layout.getLocationInfo(); |
| 157 | + int iterations = 100000; |
| 158 | + long start, stop; |
| 159 | + |
| 160 | + start = System.currentTimeMillis(); |
| 161 | + for (int i = 0; i < iterations; i++) { |
| 162 | + logger.warn("warning dawg"); |
| 163 | + } |
| 164 | + stop = System.currentTimeMillis(); |
| 165 | + long firstMeasurement = stop - start; |
| 166 | + |
| 167 | + layout.setLocationInfo(!locationInfo); |
| 168 | + start = System.currentTimeMillis(); |
| 169 | + for (int i = 0; i < iterations; i++) { |
| 170 | + logger.warn("warning dawg"); |
| 171 | + } |
| 172 | + stop = System.currentTimeMillis(); |
| 173 | + long secondMeasurement = stop - start; |
| 174 | + |
| 175 | + System.out.println("First Measurement (locationInfo: " + locationInfo + "): " + firstMeasurement); |
| 176 | + System.out.println("Second Measurement (locationInfo: " + !locationInfo + "): " + secondMeasurement); |
| 177 | + |
| 178 | + // Clean up |
| 179 | + layout.setLocationInfo(!locationInfo); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + @Ignore |
| 184 | + public void testDateFormat() { |
| 185 | + long timestamp = 1364844991207L; |
| 186 | + Assert.assertEquals("format does not produce expected output", "2013-04-01T21:36:31.207+02:00", JSONEventLayoutV1.dateFormat(timestamp)); |
| 187 | + } |
| 188 | +} |
0 commit comments