Skip to content

Commit

Permalink
Added "extraproperties" feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmpage committed Nov 8, 2016
1 parent cda5414 commit d237b8f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ specifying a custom formatter class:
<expectJson>true</expectJson>
<!-- optional -->
<includeMethodAndLineNumber>true</includeMethodAndLineNumber>
<!-- Mark every message with these additional properties.-->
<extraProperties>
serverId=SERVER-ID
domain=www.domain.com
</extraProperties>
</formatter>
</appender>
<root level="debug">
Expand Down
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.0</version>
</dependency>
<dependency>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void start() {
exception.printStackTrace();
throw new RuntimeException("KafkaAppender: Exception initializing Producer.",exception);
}
System.out.println("KafkaAppender: Producer intitialized: "+ producer);
System.out.println("KafkaAppender: Producer initialized: "+ producer);
if (topic == null) {
LOGGER.error("KafkaAppender requires a topic. Add this to the appender configuration.");
System.out.println("KafkaAppender requires a topic. Add this to the appender configuration.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import ch.qos.logback.classic.spi.ILoggingEvent;

import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Properties;

public class JsonFormatter implements Formatter {
private static final String QUOTE = "\"";
private static final String COLON = ":";
private static final String COMMA = ",";
private static final char QUOTE = '"';
private static final char COLON = ':';
private static final char COMMA = ',';

private boolean expectJson = false;
private boolean includeMethodAndLineNumber = false;
private String serverId = null;
private String extraProperties = null;

public String format(ILoggingEvent event) {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -42,10 +47,8 @@ public String format(ILoggingEvent event) {
quote(stackTraceElement.getLineNumber() + "", sb);
}
}
if(serverId!=null){
sb.append(COMMA);
fieldName("serverId", sb);
quote(serverId,sb);
if(this.extraProperties!=null){
sb.append(this.extraProperties);
}
sb.append("}");
return sb.toString();
Expand Down Expand Up @@ -78,11 +81,26 @@ public void setIncludeMethodAndLineNumber(boolean includeMethodAndLineNumber) {
this.includeMethodAndLineNumber = includeMethodAndLineNumber;
}

public String getServerId() {
return serverId;
public String getExtraProperties() {
return extraProperties;
}

public void setServerId(String serverId) {
this.serverId = serverId;
public void setExtraProperties(String thatExtraProperties) {
final Properties properties = new Properties();
try {
properties.load(new StringReader(thatExtraProperties));
} catch (IOException e) {
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 @@ -10,19 +10,27 @@ 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 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);
// 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);
// 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);
}
}

0 comments on commit d237b8f

Please sign in to comment.