diff --git a/README.md b/README.md index 8f7fcff..de07908 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,22 @@ Because of this, when adding support for the new format, `JSONEventLayoutV1` was Work has stopped on V0 but it won't be removed. No new features are added to V0 (custom UserFields for instance). +# Uuid Field +You can add a UUID to every log message in the log4jconfig: + +```xml + + + +``` + +or + +``` +log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1 +log4j.appender.RollingLog.layout.UuidField=@uuid +``` + # Custom User Fields As of version 1.6, you can now add your own metadata to the event in the form of comma-separated key:value pairs. This can be set in either the log4jconfig OR set on the java command-line: diff --git a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java index aaf3228..d430bb4 100644 --- a/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java +++ b/src/main/java/net/logstash/log4j/JSONEventLayoutV1.java @@ -13,11 +13,13 @@ import java.util.HashMap; import java.util.Map; import java.util.TimeZone; +import java.util.UUID; public class JSONEventLayoutV1 extends Layout { private boolean locationInfo = false; private String customUserFields; + private String uuidField; private boolean ignoreThrowable = false; @@ -77,6 +79,10 @@ public String format(LoggingEvent loggingEvent) { logstashEvent.put("@version", version); logstashEvent.put("@timestamp", dateFormat(timestamp)); + if (getUuidField() != null) { + logstashEvent.put(getUuidField(), UUID.randomUUID().toString()); + } + /** * Extract and add fields from log4j config, if defined */ @@ -162,6 +168,9 @@ public void setLocationInfo(boolean locationInfo) { public String getUserFields() { return customUserFields; } public void setUserFields(String userFields) { this.customUserFields = userFields; } + public String getUuidField() { return uuidField; } + public void setUuidField(String uuidField) { this.uuidField = uuidField; } + public void activateOptions() { activeIgnoreThrowable = ignoreThrowable; } diff --git a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java index 96ad821..403680c 100644 --- a/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java +++ b/src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java @@ -125,7 +125,7 @@ public void testJSONEventLayoutUserFieldsPropOverride() { Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message)); Object obj = JSONValue.parse(message); JSONObject jsonObject = (JSONObject) obj; - Assert.assertTrue("Event does not contain field 'field1'" , jsonObject.containsKey("field1")); + Assert.assertTrue("Event does not contain field 'field1'", jsonObject.containsKey("field1")); Assert.assertEquals("Event does not contain value 'propval1'", "propval1", jsonObject.get("field1")); layout.setUserFields(prevUserData); @@ -291,4 +291,32 @@ public void testDateFormat() { long timestamp = 1364844991207L; Assert.assertEquals("format does not produce expected output", "2013-04-01T19:36:31.207Z", JSONEventLayoutV1.dateFormat(timestamp)); } + + @Test + public void testJSONEventHasUuid() throws Exception { + JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout(); + layout.setUuidField("@uuid"); + + logger.info("this is an info message with @uuid field"); + String message = appender.getMessages()[0]; + Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message)); + Object obj = JSONValue.parse(message); + JSONObject jsonObject = (JSONObject) obj; + Assert.assertTrue("Event does not contain field '@uuid'" , jsonObject.containsKey("@uuid")); + Assert.assertNotNull("Field '@uuid' is null", jsonObject.get("@uuid")); + + layout.setUuidField(null); + } + + @Test + public void testJSONEventHasNotUuid() throws Exception { + JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout(); + + logger.info("this is an info message w/o @uuid field"); + String message = appender.getMessages()[0]; + Assert.assertTrue("Event is not valid JSON", JSONValue.isValidJsonStrict(message)); + Object obj = JSONValue.parse(message); + JSONObject jsonObject = (JSONObject) obj; + Assert.assertFalse("Event does not contain field '@uuid'" , jsonObject.containsKey("@uuid")); + } }