final GelfLayout layout = GelfLayout.newBuilder() .setConfiguration(ctx.getConfiguration()) .setHost(host) "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(0).getTimeMillis()) + "," + "\"level\": 7," + "\"_thread\": \"" + threadName + "\"," + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(1).getTimeMillis()) + "," + "\"level\": 6," + "\"_thread\": \"" + threadName + "\"," + inflaterStream.close(); inflaterStream2.close(); final String uncompressedString = new String(uncompressed, layout.getCharset()); final String uncompressedString2 = new String(uncompressed2, layout.getCharset()); "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(2).getTimeMillis()) + "," + "\"level\": 3," + "\"_thread\": \"" + threadName + "\"," + "\"short_message\": \"" + LINE3 + "\"," + "\"full_message\": \"" + String.valueOf(JsonStringEncoder.getInstance().quoteAsString( includeStacktrace ? GelfLayout.formatThrowable(exception).toString() : exception.toString())) + "\"," + (includeThreadContext ?
builder.append("\"version\":\"1.1\","); builder.append("\"host\":\""); JsonUtils.quoteAsString(toNullSafeString(host), builder); builder.append(QC); builder.append("\"timestamp\":").append(formatTimestamp(event.getTimeMillis())).append(C); builder.append("\"level\":").append(formatLevel(event.getLevel())).append(C); if (event.getThreadName() != null) { builder.append("\"_thread\":\""); final StrSubstitutor strSubstitutor = getConfiguration().getStrSubstitutor(); for (final KeyValuePair additionalField : additionalFields) { builder.append(QU); JsonUtils.quoteAsString(additionalField.getKey(), builder); builder.append("\":\""); final String value = valueNeedsLookup(additionalField.getValue()) ? strSubstitutor.replace(event, additionalField.getValue()) : additionalField.getValue(); JsonUtils.quoteAsString(toNullSafeString(value), builder); builder.append(QC); builder.append("\"full_message\":\""); if (includeStacktrace) { JsonUtils.quoteAsString(formatThrowable(event.getThrown()), builder); } else { JsonUtils.quoteAsString(event.getThrown().toString(), builder); JsonUtils.quoteAsString(((CharSequence)message), builder); } else if (gcFree && message instanceof StringBuilderFormattable) { final StringBuilder messageBuffer = getMessageStringBuilder();
@Override public byte[] toByteArray(final LogEvent event) { final StringBuilder text = toText(event, getStringBuilder(), false); final byte[] bytes = getBytes(text.toString()); return compressionType != CompressionType.OFF && bytes.length > compressionThreshold ? compress(bytes) : bytes; }
@Override public void encode(final LogEvent event, final ByteBufferDestination destination) { if (compressionType != CompressionType.OFF) { super.encode(event, destination); return; } final StringBuilder text = toText(event, getStringBuilder(), true); final Encoder<StringBuilder> helper = getStringBuilderEncoder(); helper.encode(text, destination); }
@Test public void testFormatTimestamp() { assertEquals("0", GelfLayout.formatTimestamp(0L).toString()); assertEquals("1.000", GelfLayout.formatTimestamp(1000L).toString()); assertEquals("1.001", GelfLayout.formatTimestamp(1001L).toString()); assertEquals("1.010", GelfLayout.formatTimestamp(1010L).toString()); assertEquals("1.100", GelfLayout.formatTimestamp(1100L).toString()); assertEquals("1458741206.653", GelfLayout.formatTimestamp(1458741206653L).toString()); assertEquals("9223372036854775.807", GelfLayout.formatTimestamp(Long.MAX_VALUE).toString()); } }
@Override public String getContentType() { return JsonLayout.CONTENT_TYPE + "; charset=" + this.getCharset(); }
@Override public String toSerializable(final LogEvent event) { final StringBuilder text = toText(event, getStringBuilder(), false); return text.toString(); }
@Override public GelfLayout build() { return new GelfLayout(getConfiguration(), host, additionalFields, compressionType, compressionThreshold, includeStacktrace, includeThreadContext, includeNullDelimiter); }
/** * @deprecated Use {@link #newBuilder()} instead */ @Deprecated public static GelfLayout createLayout( //@formatter:off @PluginAttribute("host") final String host, @PluginElement("AdditionalField") final KeyValuePair[] additionalFields, @PluginAttribute(value = "compressionType", defaultString = "GZIP") final CompressionType compressionType, @PluginAttribute(value = "compressionThreshold", defaultInt = COMPRESSION_THRESHOLD) final int compressionThreshold, @PluginAttribute(value = "includeStacktrace", defaultBoolean = true) final boolean includeStacktrace) { // @formatter:on return new GelfLayout(null, host, additionalFields, compressionType, compressionThreshold, includeStacktrace, true, false); }