Skip to content

Commit

Permalink
Added support for Marker serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
afinkenstadt committed Apr 4, 2016
1 parent 5c5e727 commit 127bd1c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.splunk.logging</groupId>
<artifactId>splunk-library-javalogging</artifactId>
<version>1.5.2-develop-2</version>
<version>1.5.2-develop-3</version>
<packaging>jar</packaging>

<name>Splunk Logging for Java</name>
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/splunk/logging/HttpEventCollectorEventInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -34,20 +34,30 @@ public class HttpEventCollectorEventInfo {
private final String thread_name;
private final Map<String, String> 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<String, String> properties, final IThrowableProxy thrown) {
public HttpEventCollectorEventInfo(
final String severity,
final String message,
final String logger_name,
final String thread_name,
final Map<String, String> properties,
final IThrowableProxy thrown,
final Serializable marker
) {
this.time = System.currentTimeMillis() / 1000.0;
this.severity = severity;
this.message = message;
this.logger_name = logger_name;
this.thread_name = thread_name;
this.properties = properties;
this.thrown = thrown;
this.marker = marker;
}

/**
Expand Down Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,14 +96,16 @@ 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(),
_layout.doLayout(event),
event.getLoggerName(),
event.getThreadName(),
event.getMDCPropertyMap(),
event.getThrowableProxy()
event.getThrowableProxy(),
c.convert(event)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/splunk/logging/HttpEventCollectorSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, String> properties, IThrowableProxy thrown) {
public synchronized void send(
final String severity,
final String message,
final String logger_name,
final String thread_name,
Map<String, String> 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) {
Expand Down Expand Up @@ -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<String,String> 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();
Expand Down

0 comments on commit 127bd1c

Please sign in to comment.