Skip to content

Commit

Permalink
Added json-simple dependency. Revised JSONFormatter to use JSONObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
John at Work committed Nov 8, 2016
1 parent d237b8f commit 5448915
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target/
.idea
*.iml
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
<scope>provided</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.0</version>
</dependency><!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5448915

Please sign in to comment.