Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UuidField parameter to inject UUID for every log message. #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<layout class="net.logstash.log4j.JSONEventLayoutV1" >
<param name="UuidField" value="@uuid" />
</layout>
```

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:

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand Down
30 changes: 29 additions & 1 deletion src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
}
}