From bc8d486e8c87953235eb9041277990a12e7249c0 Mon Sep 17 00:00:00 2001 From: Daniel Fedorowsky Date: Thu, 2 Aug 2018 12:08:22 +0300 Subject: [PATCH] Added support for JSON parsing (#49) * 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 --- README.md | 2 +- .../logz/logback/LogzioLogbackAppender.java | 30 +++++++++++++- .../logz/logback/BaseLogbackAppenderTest.java | 2 + .../logback/LogzioLogbackAppenderTest.java | 39 ++++++++++++++++--- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 12a7826..900d372 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/java/io/logz/logback/LogzioLogbackAppender.java b/src/main/java/io/logz/logback/LogzioLogbackAppender.java index 9dee89c..4d86098 100644 --- a/src/main/java/io/logz/logback/LogzioLogbackAppender.java +++ b/src/main/java/io/logz/logback/LogzioLogbackAppender.java @@ -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; @@ -22,6 +24,7 @@ public class LogzioLogbackAppender extends UnsynchronizedAppenderBase { + 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"; @@ -30,6 +33,8 @@ public class LogzioLogbackAppender extends UnsynchronizedAppenderBase reservedFields = new HashSet<>(Arrays.asList(new String[] {TIMESTAMP,LOGLEVEL, MARKER, MESSAGE,LOGGER,THREAD,EXCEPTION})); @@ -52,11 +57,20 @@ public class LogzioLogbackAppender extends UnsynchronizedAppenderBase 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"; @@ -265,7 +293,6 @@ public void testTokenAndLogzioUrlFromSystemEnvironment() { mockListener.assertLogReceivedIs(logRequest, token, type, loggerName, Level.INFO.levelStr); } - public void assertAdditionalFields(MockLogzioBulkListener.LogRequest logRequest, Map additionalFields) { additionalFields.forEach((field, value) -> { String fieldValueInLog = logRequest.getStringFieldOrNull(field);