Search in sources :

Example 1 with EncodingListAppender

use of org.apache.logging.log4j.core.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, final boolean includeNewLineDelimiter) 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).setIncludeNewLineDelimiter(includeNewLineDelimiter).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);
    if (includeNullDelimiter) {
        assertEquals(uncompressedString.indexOf('\0'), uncompressedString.length() - 1);
        assertEquals(uncompressedString2.indexOf('\0'), uncompressedString2.length() - 1);
    }
    if (includeNewLineDelimiter) {
        assertEquals(uncompressedString.indexOf('\n'), uncompressedString.length() - 1);
        assertEquals(uncompressedString2.indexOf('\n'), uncompressedString2.length() - 1);
    }
}
Also used : ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) EncodingListAppender(org.apache.logging.log4j.core.test.appender.EncodingListAppender) KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) JavaLookup(org.apache.logging.log4j.core.lookup.JavaLookup) EncodingListAppender(org.apache.logging.log4j.core.test.appender.EncodingListAppender) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ListAppender(org.apache.logging.log4j.core.test.appender.ListAppender) EncodingListAppender(org.apache.logging.log4j.core.test.appender.EncodingListAppender)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 JavaLookup (org.apache.logging.log4j.core.lookup.JavaLookup)1 EncodingListAppender (org.apache.logging.log4j.core.test.appender.EncodingListAppender)1 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)1 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)1