Skip to content

Commit

Permalink
Added support for JSON parsing (#49)
Browse files Browse the repository at this point in the history
* Added Json parsing support

* Catch all exceptions during Json parsing support

* Reverted pom.xml

* Added testing for JSON messages

* Added testing for JSON messages

* Remove unutilized import

* Changes after pull request review. Added format parameter and update the readme

* Fixed imports
  • Loading branch information
DanielFedorowsky authored and idohalevi committed Aug 2, 2018
1 parent 0c8c33d commit bc8d486
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This appender uses [LogzioSender](https://github.com/logzio/logzio-java-sender)
| **debug** | *false* | Print some debug messages to stdout to help to diagnose issues |
| **line** | *false* | Print the line of code that generated this log |
| **compressRequests** | *false* | Boolean. `true` if logs are compressed in gzip format before sending. `false` if logs are sent uncompressed. |

| **format** | *text* | Optional. `json` if the logged message is to be parsed as a JSON (in such a way that each JSON node will be a field in logz.io) or `text` if the logged message is to be treated as plain text.

### Code Example
```java
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/io/logz/logback/LogzioLogbackAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.google.common.base.Splitter;
import io.logz.sender.LogzioSender;
import io.logz.sender.SenderStatusReporter;
import io.logz.sender.com.google.gson.Gson;
import io.logz.sender.com.google.gson.JsonElement;
import io.logz.sender.com.google.gson.JsonObject;
import io.logz.sender.exceptions.LogzioParameterErrorException;

Expand All @@ -22,6 +24,7 @@

public class LogzioLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

private static final Gson gson = new Gson();
private static final String TIMESTAMP = "@timestamp";
private static final String LOGLEVEL = "loglevel";
private static final String MARKER = "marker";
Expand All @@ -30,6 +33,8 @@ public class LogzioLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEv
private static final String LINE = "line";
private static final String THREAD = "thread";
private static final String EXCEPTION = "exception";
private static final String FORMAT_TEXT = "text";
private static final String FORMAT_JSON = "json";

private static final Set<String> reservedFields = new HashSet<>(Arrays.asList(new String[] {TIMESTAMP,LOGLEVEL, MARKER, MESSAGE,LOGGER,THREAD,EXCEPTION}));

Expand All @@ -52,11 +57,20 @@ public class LogzioLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEv
private boolean line = false;
private boolean compressRequests = false;
private int gcPersistedQueueFilesIntervalSeconds = 30;
private String format = FORMAT_TEXT;

public LogzioLogbackAppender() {
super();
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public void setToken(String logzioToken) {
this.logzioToken = getValueFromSystemEnvironmentIfNeeded(logzioToken);
}
Expand Down Expand Up @@ -220,7 +234,20 @@ private String getValueFromSystemEnvironmentIfNeeded(String value) {
}

private JsonObject formatMessageAsJson(ILoggingEvent loggingEvent) {
JsonObject logMessage = new JsonObject();
JsonObject logMessage;

if (format.equals(FORMAT_JSON)) {
try {
JsonElement jsonElement = gson.fromJson(loggingEvent.getFormattedMessage(), JsonElement.class);
logMessage = jsonElement.getAsJsonObject();
} catch (Exception e) {
logMessage = new JsonObject();
logMessage.addProperty(MESSAGE, loggingEvent.getFormattedMessage());
}
} else {
logMessage = new JsonObject();
logMessage.addProperty(MESSAGE, loggingEvent.getFormattedMessage());
}

// Adding MDC first, as I dont want it to collide with any one of the following fields
if (loggingEvent.getMDCPropertyMap() != null) {
Expand All @@ -234,7 +261,6 @@ private JsonObject formatMessageAsJson(ILoggingEvent loggingEvent) {
logMessage.addProperty(MARKER, loggingEvent.getMarker().toString());
}

logMessage.addProperty(MESSAGE, loggingEvent.getFormattedMessage());
logMessage.addProperty(LOGGER, loggingEvent.getLoggerName());
logMessage.addProperty(THREAD, loggingEvent.getThreadName());
if (line) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/io/logz/logback/BaseLogbackAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -47,6 +48,7 @@ protected Logger createLogger(String token, String type, String loggerName, Inte
logzioLogbackAppender.setLogzioUrl("http://" + mockListener.getHost() + ":" + mockListener.getPort());
logzioLogbackAppender.setAddHostname(addHostname);
logzioLogbackAppender.setCompressRequests(compressRequests);
logzioLogbackAppender.setName("LogzioLogbackAppender");
if (drainTimeout != null) {
logzioLogbackAppender.setDrainTimeoutSec(drainTimeout);
}
Expand Down
39 changes: 33 additions & 6 deletions src/test/java/io/logz/logback/LogzioLogbackAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import io.logz.sender.com.google.gson.Gson;
import io.logz.test.MockLogzioBulkListener;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.*;

import java.net.InetAddress;
import java.util.HashMap;
Expand All @@ -23,6 +20,37 @@ public class LogzioLogbackAppenderTest extends BaseLogbackAppenderTest {

private final static Logger logger = LoggerFactory.getLogger(LogzioLogbackAppenderTest.class);

@Test
public void validateJsonMessage(){
String token = "validatingAdditionalFields";
String type = "willTryWithOrWithoutEnvironmentVariables";
String loggerName = "additionalLogger";
int drainTimeout = 1;
String messageText = "message test";

Map<String, Object> map = new HashMap<>();
map.put("message", messageText);
map.put("userName", "test");
map.put("email", "test@email.com");

String message1 = new Gson().toJson(map);

Logger testLogger = createLogger(token, type, loggerName, drainTimeout, false, false, null);
LogzioLogbackAppender logzioLogbackAppender =
(LogzioLogbackAppender)((ch.qos.logback.classic.Logger)testLogger).getAppender("LogzioLogbackAppender");
logzioLogbackAppender.setFormat("json");
testLogger.info(message1);

sleepSeconds(2 * drainTimeout);

mockListener.assertNumberOfReceivedMsgs(1);
MockLogzioBulkListener.LogRequest logRequest = mockListener.assertLogReceivedByMessage(messageText);
mockListener.assertLogReceivedIs(logRequest, token, type, loggerName, Level.INFO.levelStr);

assertThat(logRequest.getStringFieldOrNull("userName")).isNotNull().isEqualTo("test");
assertThat(logRequest.getStringFieldOrNull("email")).isNotNull().isEqualTo("test@email.com");
}

@Test
public void simpleAppending() throws Exception {
String token = "aBcDeFgHiJkLmNoPqRsT";
Expand Down Expand Up @@ -265,7 +293,6 @@ public void testTokenAndLogzioUrlFromSystemEnvironment() {
mockListener.assertLogReceivedIs(logRequest, token, type, loggerName, Level.INFO.levelStr);
}


public void assertAdditionalFields(MockLogzioBulkListener.LogRequest logRequest, Map<String, String> additionalFields) {
additionalFields.forEach((field, value) -> {
String fieldValueInLog = logRequest.getStringFieldOrNull(field);
Expand Down

0 comments on commit bc8d486

Please sign in to comment.