diff --git a/.gitignore b/.gitignore
index 2f7896d..e02456b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
target/
+.idea
+*.iml
diff --git a/pom.xml b/pom.xml
index 95e7f66..d00a324 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,12 +63,17 @@
ch.qos.logback
logback-classic
1.1.7
- provided
+ provided
org.apache.kafka
kafka-clients
0.10.0.0
+
+
+ com.googlecode.json-simple
+ json-simple
+ 1.1.1
\ No newline at end of file
diff --git a/src/main/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatter.java b/src/main/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatter.java
index c3c4243..e2c5168 100644
--- a/src/main/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatter.java
+++ b/src/main/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatter.java
@@ -1,76 +1,53 @@
package com.github.ptgoetz.logback.kafka.formatter;
import ch.qos.logback.classic.spi.ILoggingEvent;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
public class JsonFormatter implements Formatter {
- private static final char QUOTE = '"';
- private static final char COLON = ':';
- private static final char COMMA = ',';
- private boolean expectJson = false;
+ private boolean expectJsonMessage = false;
private boolean includeMethodAndLineNumber = false;
- private String extraProperties = null;
+ private Map extraPropertiesMap = null;
public String format(ILoggingEvent event) {
- StringBuilder sb = new StringBuilder();
- sb.append("{");
- fieldName("level", sb);
- quote(event.getLevel().levelStr, sb);
- sb.append(COMMA);
- fieldName("logger", sb);
- quote(event.getLoggerName(), sb);
- sb.append(COMMA);
- fieldName("timestamp", sb);
- sb.append(event.getTimeStamp());
- sb.append(COMMA);
- fieldName("message", sb);
- if (this.expectJson) {
- sb.append(event.getFormattedMessage());
- } else {
- quote(event.getFormattedMessage(), sb);
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("level", event.getLevel().levelStr);
+ jsonObject.put("logger", event.getLoggerName());
+ jsonObject.put("timestamp", event.getTimeStamp());
+ if( expectJsonMessage ) {
+ Object object = JSONValue.parse(event.getFormattedMessage());
+ jsonObject.put("message", object );
+ }else{
+ jsonObject.put("message", event.getFormattedMessage());
}
- if(includeMethodAndLineNumber) {
- sb.append(COMMA);
- // Caller Data
+ if (includeMethodAndLineNumber) {
StackTraceElement[] callerDataArray = event.getCallerData();
if (callerDataArray != null && callerDataArray.length > 0) {
StackTraceElement stackTraceElement = callerDataArray[0];
- fieldName("method", sb);
- quote(stackTraceElement.getMethodName(), sb);
- sb.append(COMMA);
- fieldName("lineNumber", sb);
- quote(stackTraceElement.getLineNumber() + "", sb);
+ jsonObject.put("method", stackTraceElement.getMethodName());
+ jsonObject.put("lineNumber", stackTraceElement.getLineNumber() + "");
}
}
- if(this.extraProperties!=null){
- sb.append(this.extraProperties);
+ if (this.extraPropertiesMap != null) {
+ jsonObject.putAll(extraPropertiesMap);
}
- sb.append("}");
- return sb.toString();
+ return jsonObject.toJSONString();
}
- private static void fieldName(String name, StringBuilder sb) {
- quote(name, sb);
- sb.append(COLON);
+ public boolean getExpectJsonMessage() {
+ return expectJsonMessage;
}
- private static void quote(String value, StringBuilder sb) {
- sb.append(QUOTE);
- sb.append(value);
- sb.append(QUOTE);
- }
-
- public boolean getExpectJson() {
- return expectJson;
- }
-
- public void setExpectJson(boolean expectJson) {
- this.expectJson = expectJson;
+ public void setExpectJsonMessage(boolean expectJsonMessage) {
+ this.expectJsonMessage = expectJsonMessage;
}
public boolean getIncludeMethodAndLineNumber() {
@@ -81,26 +58,20 @@ public void setIncludeMethodAndLineNumber(boolean includeMethodAndLineNumber) {
this.includeMethodAndLineNumber = includeMethodAndLineNumber;
}
- public String getExtraProperties() {
- return extraProperties;
- }
-
public void setExtraProperties(String thatExtraProperties) {
final Properties properties = new Properties();
try {
properties.load(new StringReader(thatExtraProperties));
+ Enumeration> enumeration = properties.propertyNames();
+ extraPropertiesMap = new HashMap();
+ while(enumeration.hasMoreElements()){
+ String name = (String)enumeration.nextElement();
+ String value = properties.getProperty(name);
+ extraPropertiesMap.put(name,value);
+ }
} catch (IOException e) {
+ System.out.println("There was a problem reading the extra properties configuration: "+e.getMessage());
e.printStackTrace();
}
- Enumeration> enumeration = properties.propertyNames();
- StringBuilder sb = new StringBuilder();
- while(enumeration.hasMoreElements()){
- String name = (String)enumeration.nextElement();
- String value = properties.getProperty(name);
- sb.append(COMMA);
- fieldName(name, sb);
- quote(value,sb);
- }
- this.extraProperties = sb.toString();
}
}
\ No newline at end of file
diff --git a/src/test/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatterTest.java b/src/test/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatterTest.java
index 1bc8c67..6979aa2 100644
--- a/src/test/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatterTest.java
+++ b/src/test/java/com/github/ptgoetz/logback/kafka/formatter/JsonFormatterTest.java
@@ -3,34 +3,35 @@
import org.junit.Test;
import static junit.framework.Assert.*;
+
import junit.framework.TestCase;
public class JsonFormatterTest extends TestCase {
@Test
public void testJsonFormat() {
- String nonJsonMessage = "{\"level\":\"INFO\",\"logger\":\"test\",\"timestamp\":1370918376296,\"message\":\"foobar\"}";
- String jsonMessage = "{\"level\":\"INFO\",\"logger\":\"test\",\"timestamp\":1370918376296,\"message\":{\"foo\":\"bar\"}}";
- String nonJsonMessageWithGroupProperties = "{\"level\":\"INFO\",\"logger\":\"test\",\"timestamp\":1370918376296,\"message\":\"foobar\",\"name2\":\"value2\",\"name1\":\"value1\"}";
-
- // non-JSON
- MockLoggingEvent event = new MockLoggingEvent(false);
- JsonFormatter formatter = new JsonFormatter();
- formatter.setExpectJson(false);
- String json = formatter.format(event);
- assertEquals(nonJsonMessage, json);
-
- // JSON
- event = new MockLoggingEvent(true);
- formatter.setExpectJson(true);
- json = formatter.format(event);
- assertEquals(jsonMessage, json);
-
- // Group Properties
- event = new MockLoggingEvent(false);
- formatter.setExpectJson(false);
- formatter.setGroupProperties("name1=value1\nname2=value2");
- json = formatter.format(event);
- assertEquals(nonJsonMessageWithGroupProperties, json);
+ String nonJsonMessage = "{\"level\":\"INFO\",\"logger\":\"test\",\"message\":\"foobar\",\"timestamp\":1370918376296}";
+ String jsonMessage = "{\"level\":\"INFO\",\"logger\":\"test\",\"message\":{\"foo\":\"bar\"},\"timestamp\":1370918376296}";
+ String nonJsonMessageWithGroupProperties = "{\"level\":\"INFO\",\"logger\":\"test\",\"message\":\"foobar\",\"name2\":\"value2\",\"name1\":\"value1\",\"timestamp\":1370918376296}";
+
+ // non-JSON
+ MockLoggingEvent event = new MockLoggingEvent(false);
+ JsonFormatter formatter = new JsonFormatter();
+ formatter.setExpectJsonMessage(false);
+ String json = formatter.format(event);
+ assertEquals(nonJsonMessage, json);
+
+ // JSON
+ event = new MockLoggingEvent(true);
+ formatter.setExpectJsonMessage(true);
+ json = formatter.format(event);
+ assertEquals(jsonMessage, json);
+
+ // Extra Properties
+ event = new MockLoggingEvent(false);
+ formatter.setExpectJsonMessage(false);
+ formatter.setExtraProperties("name1=value1\nname2=value2");
+ json = formatter.format(event);
+ assertEquals(nonJsonMessageWithGroupProperties, json);
}
}