diff --git a/README.md b/README.md index a15d65e..b5579e6 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,11 @@ specifying a custom formatter class: true true + + + serverId=SERVER-ID + domain=www.domain.com + diff --git a/pom.xml b/pom.xml index 7cd9733..95e7f66 100644 --- a/pom.xml +++ b/pom.xml @@ -63,16 +63,12 @@ ch.qos.logback logback-classic 1.1.7 + provided org.apache.kafka kafka-clients 0.10.0.0 - - slf4j-api - org.slf4j - 1.7.5 - \ No newline at end of file diff --git a/src/main/java/com/github/ptgoetz/logback/kafka/KafkaAppender.java b/src/main/java/com/github/ptgoetz/logback/kafka/KafkaAppender.java index 895be80..72e084d 100644 --- a/src/main/java/com/github/ptgoetz/logback/kafka/KafkaAppender.java +++ b/src/main/java/com/github/ptgoetz/logback/kafka/KafkaAppender.java @@ -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."); 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 3ed600f..c3c4243 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 @@ -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(); @@ -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(); @@ -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(); } } \ 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 26b2e9d..1bc8c67 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 @@ -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); } }