From 127bd1c7d2da3e6fab4f67e47a1d4b0c65e2c641 Mon Sep 17 00:00:00 2001 From: Andy Finkenstadt Date: Mon, 4 Apr 2016 12:53:30 -0700 Subject: [PATCH] Added support for Marker serialization --- pom.xml | 2 +- .../logging/HttpEventCollectorEventInfo.java | 19 +++++++++++++-- .../HttpEventCollectorLog4jAppender.java | 3 ++- .../HttpEventCollectorLogbackAppender.java | 5 +++- .../HttpEventCollectorLoggingHandler.java | 3 ++- .../logging/HttpEventCollectorSender.java | 23 +++++++++++++++---- 6 files changed, 44 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 859d67fc..b2e4c67f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.splunk.logging splunk-library-javalogging - 1.5.2-develop-2 + 1.5.2-develop-3 jar Splunk Logging for Java diff --git a/src/main/java/com/splunk/logging/HttpEventCollectorEventInfo.java b/src/main/java/com/splunk/logging/HttpEventCollectorEventInfo.java index 256b065d..8bbfc0ac 100644 --- a/src/main/java/com/splunk/logging/HttpEventCollectorEventInfo.java +++ b/src/main/java/com/splunk/logging/HttpEventCollectorEventInfo.java @@ -19,8 +19,8 @@ */ import ch.qos.logback.classic.spi.IThrowableProxy; -import ch.qos.logback.classic.spi.ThrowableProxy; +import java.io.Serializable; import java.util.Map; /** @@ -34,13 +34,22 @@ public class HttpEventCollectorEventInfo { private final String thread_name; private final Map properties; private final IThrowableProxy thrown; + private final Serializable marker; /** * Create a new HttpEventCollectorEventInfo container * @param severity of event * @param message is an event content */ - public HttpEventCollectorEventInfo(final String severity, final String message, final String logger_name, final String thread_name, final Map properties, final IThrowableProxy thrown) { + public HttpEventCollectorEventInfo( + final String severity, + final String message, + final String logger_name, + final String thread_name, + final Map properties, + final IThrowableProxy thrown, + final Serializable marker + ) { this.time = System.currentTimeMillis() / 1000.0; this.severity = severity; this.message = message; @@ -48,6 +57,7 @@ public HttpEventCollectorEventInfo(final String severity, final String message, this.thread_name = thread_name; this.properties = properties; this.thrown = thrown; + this.marker = marker; } /** @@ -90,4 +100,9 @@ public final String getMessage() { * @return event thrown exception */ public IThrowableProxy getThrown() { return thrown; } + + /** + * @return event marker + */ + public Serializable getMarker() { return marker; } } \ No newline at end of file diff --git a/src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java b/src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java index 5b8f5100..0c1b7137 100644 --- a/src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java +++ b/src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java @@ -163,7 +163,8 @@ public void append(final LogEvent event) event.getLoggerName(), event.getThreadName(), event.getContextMap(), - event.getThrown() == null ? null : new ThrowableProxy(event.getThrown()) + event.getThrown() == null ? null : new ThrowableProxy(event.getThrown()), + event.getMarker() ); } diff --git a/src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java b/src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java index 46e157a1..1c88aaca 100644 --- a/src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java +++ b/src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java @@ -15,6 +15,7 @@ * under the License. */ +import ch.qos.logback.classic.pattern.MarkerConverter; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.Layout; @@ -95,6 +96,7 @@ public void stop() { protected void append(ILoggingEvent event) { event.prepareForDeferredProcessing(); event.getCallerData(); + MarkerConverter c = new MarkerConverter(); if (event != null && started) { this.sender.send( event.getLevel().toString(), @@ -102,7 +104,8 @@ protected void append(ILoggingEvent event) { event.getLoggerName(), event.getThreadName(), event.getMDCPropertyMap(), - event.getThrowableProxy() + event.getThrowableProxy(), + c.convert(event) ); } } diff --git a/src/main/java/com/splunk/logging/HttpEventCollectorLoggingHandler.java b/src/main/java/com/splunk/logging/HttpEventCollectorLoggingHandler.java index 76485c96..4720e33d 100644 --- a/src/main/java/com/splunk/logging/HttpEventCollectorLoggingHandler.java +++ b/src/main/java/com/splunk/logging/HttpEventCollectorLoggingHandler.java @@ -167,7 +167,8 @@ public void publish(LogRecord record) { record.getLoggerName(), String.format(Locale.US, "%d", record.getThreadID()), null, // no property map available - record.getThrown() == null ? null : new ThrowableProxy(record.getThrown()) + record.getThrown() == null ? null : new ThrowableProxy(record.getThrown()), + null // no marker available ); } diff --git a/src/main/java/com/splunk/logging/HttpEventCollectorSender.java b/src/main/java/com/splunk/logging/HttpEventCollectorSender.java index 26ac3b60..b1bfcada 100644 --- a/src/main/java/com/splunk/logging/HttpEventCollectorSender.java +++ b/src/main/java/com/splunk/logging/HttpEventCollectorSender.java @@ -19,7 +19,6 @@ */ import ch.qos.logback.classic.spi.IThrowableProxy; -import ch.qos.logback.classic.spi.ThrowableProxy; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.concurrent.FutureCallback; @@ -30,10 +29,11 @@ import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.util.EntityUtils; -import org.apache.logging.log4j.core.appender.db.jpa.converter.ThrowableAttributeConverter; + import org.json.simple.JSONObject; import javax.net.ssl.SSLContext; import java.io.IOException; +import java.io.Serializable; import java.security.cert.X509Certificate; import java.util.Dictionary; import java.util.Timer; @@ -144,10 +144,18 @@ public void addMiddleware(HttpEventCollectorMiddleware.HttpSenderMiddleware midd * @param severity event severity level (info, warning, etc.) * @param message event text */ - public synchronized void send(final String severity, final String message, final String logger_name, final String thread_name, Map properties, IThrowableProxy thrown) { + public synchronized void send( + final String severity, + final String message, + final String logger_name, + final String thread_name, + Map properties, + IThrowableProxy thrown, + Serializable marker + ) { // create event info container and add it to the batch HttpEventCollectorEventInfo eventInfo = - new HttpEventCollectorEventInfo(severity, message, logger_name, thread_name, properties, thrown); + new HttpEventCollectorEventInfo(severity, message, logger_name, thread_name, properties, thrown, marker); eventsBatch.add(eventInfo); eventsBatchSize += severity.length() + message.length(); if (eventsBatch.size() >= maxEventsBatchCount || eventsBatchSize > maxEventsBatchSize) { @@ -225,12 +233,17 @@ private String serializeEventInfo(HttpEventCollectorEventInfo eventInfo) { if (eventInfo.getThrown() != null) { putIfPresent(body, "exception", eventInfo.getThrown().getMessage()); } + // add properties if and only if there are any final Map props = eventInfo.getProperties(); if (props != null && !props.isEmpty()) { body.put("properties", props); } - + // add marker if and only if there is one + final Serializable marker = eventInfo.getMarker(); + if (marker != null) { + putIfPresent(body, "marker", marker.toString()); + } // join event and body event.put("event", body); return event.toString();