use of org.apache.logging.log4j.test.appender.EncodingListAppender in project logging-log4j2 by apache.
the class GelfLayoutTest method testCompressedLayout.
private void testCompressedLayout(final CompressionType compressionType, final boolean includeStacktrace, final boolean includeThreadContext, String host, final boolean includeNullDelimiter) throws IOException {
for (final Appender appender : root.getAppenders().values()) {
root.removeAppender(appender);
}
// set up appenders
final GelfLayout layout = GelfLayout.newBuilder().setConfiguration(ctx.getConfiguration()).setHost(host).setAdditionalFields(new KeyValuePair[] { new KeyValuePair(KEY1, VALUE1), new KeyValuePair(KEY2, "${java:runtime}") }).setCompressionType(compressionType).setCompressionThreshold(1024).setIncludeStacktrace(includeStacktrace).setIncludeThreadContext(includeThreadContext).setIncludeNullDelimiter(includeNullDelimiter).build();
final ListAppender eventAppender = new ListAppender("Events", null, null, true, false);
final ListAppender rawAppender = new ListAppender("Raw", null, layout, true, true);
final ListAppender formattedAppender = new ListAppender("Formatted", null, layout, true, false);
final EncodingListAppender encodedAppender = new EncodingListAppender("Encoded", null, layout, false, true);
eventAppender.start();
rawAppender.start();
formattedAppender.start();
encodedAppender.start();
if (host == null)
host = NetUtils.getLocalHostname();
final JavaLookup javaLookup = new JavaLookup();
// set appenders on root and set level to debug
root.addAppender(eventAppender);
root.addAppender(rawAppender);
root.addAppender(formattedAppender);
root.addAppender(encodedAppender);
root.setLevel(Level.DEBUG);
root.debug(LINE1);
ThreadContext.put(MDCKEY1, MDCVALUE1);
ThreadContext.put(MDCKEY2, MDCVALUE2);
root.info(LINE2);
final Exception exception = new RuntimeException("some error");
root.error(LINE3, exception);
formattedAppender.stop();
final List<LogEvent> events = eventAppender.getEvents();
final List<byte[]> raw = rawAppender.getData();
final List<String> messages = formattedAppender.getMessages();
final List<byte[]> raw2 = encodedAppender.getData();
final String threadName = Thread.currentThread().getName();
//@formatter:off
assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(0).getTimeMillis()) + "," + "\"level\": 7," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE1 + "\"," + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}", messages.get(0));
assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(1).getTimeMillis()) + "," + "\"level\": 6," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE2 + "\"," + (includeThreadContext ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," : "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}", messages.get(1));
//@formatter:on
final byte[] compressed = raw.get(2);
final byte[] compressed2 = raw2.get(2);
final ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
final ByteArrayInputStream bais2 = new ByteArrayInputStream(compressed2);
InputStream inflaterStream;
InputStream inflaterStream2;
switch(compressionType) {
case GZIP:
inflaterStream = new GZIPInputStream(bais);
inflaterStream2 = new GZIPInputStream(bais2);
break;
case ZLIB:
inflaterStream = new InflaterInputStream(bais);
inflaterStream2 = new InflaterInputStream(bais2);
break;
case OFF:
inflaterStream = bais;
inflaterStream2 = bais2;
break;
default:
throw new IllegalStateException("Missing test case clause");
}
final byte[] uncompressed = IOUtils.toByteArray(inflaterStream);
final byte[] uncompressed2 = IOUtils.toByteArray(inflaterStream2);
inflaterStream.close();
inflaterStream2.close();
final String uncompressedString = new String(uncompressed, layout.getCharset());
final String uncompressedString2 = new String(uncompressed2, layout.getCharset());
//@formatter:off
final String expected = "{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(2).getTimeMillis()) + "," + "\"level\": 3," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE3 + "\"," + "\"full_message\": \"" + String.valueOf(JsonStringEncoder.getInstance().quoteAsString(includeStacktrace ? GelfLayout.formatThrowable(exception).toString() : exception.toString())) + "\"," + (includeThreadContext ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," : "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}";
//@formatter:on
assertJsonEquals(expected, uncompressedString);
assertJsonEquals(expected, uncompressedString2);
}
Aggregations