diff --git a/src/main/java/biz/paluch/logging/gelf/GelfMessageAssembler.java b/src/main/java/biz/paluch/logging/gelf/GelfMessageAssembler.java
index 3955f6971..54df19ddd 100644
--- a/src/main/java/biz/paluch/logging/gelf/GelfMessageAssembler.java
+++ b/src/main/java/biz/paluch/logging/gelf/GelfMessageAssembler.java
@@ -197,7 +197,9 @@ private void setupStaticFields(PropertyProvider propertyProvider) {
}
public void addField(MessageField field) {
- this.fields.add(field);
+ if (!fields.contains(field)) {
+ this.fields.add(field);
+ }
}
public void addFields(Collection extends MessageField> fields) {
diff --git a/src/main/java/biz/paluch/logging/gelf/wildfly/WildFlyJsonFormatter.java b/src/main/java/biz/paluch/logging/gelf/wildfly/WildFlyJsonFormatter.java
index 0fdd1bc35..d05790a1e 100644
--- a/src/main/java/biz/paluch/logging/gelf/wildfly/WildFlyJsonFormatter.java
+++ b/src/main/java/biz/paluch/logging/gelf/wildfly/WildFlyJsonFormatter.java
@@ -21,6 +21,8 @@
import biz.paluch.logging.gelf.intern.GelfMessage;
import biz.paluch.logging.gelf.jboss7.JBoss7JulLogEvent;
+import java.util.*;
+
/**
* Log-Formatter for JSON using fields specified within GELF. This formatter will produce a JSON object for each log event.
* Example:
@@ -46,6 +48,8 @@
* Following parameters are supported/needed:
*
* - lineBreak (Optional): End of line, defaults to {@code \n}
+ * - fields (Optional): Comma-separated list of log event fields that should be included in the JSON. Defaults to
+ * {@code Time, Severity, ThreadName, SourceClassName, SourceMethodName, SourceSimpleClassName, LoggerName, NDC}
* - originHost (Optional): Originating Hostname, default FQDN Hostname
* - extractStacktrace (Optional): Post Stack-Trace to StackTrace field, default false
* - filterStackTrace (Optional): Perform Stack-Trace filtering (true/false), default false
@@ -76,26 +80,81 @@
*/
public class WildFlyJsonFormatter extends ExtFormatter {
+ public static final String MULTI_VALUE_DELIMITTER = ",";
private MdcGelfMessageAssembler gelfMessageAssembler = new MdcGelfMessageAssembler();
private String lineBreak = "\n";
+ private boolean wasSetFieldsCalled = false;
+
+ public static final Set SUPPORTED_FIELDS;
+
+ static {
+ Set supportedFields = new LinkedHashSet();
+
+ supportedFields.add(Time);
+ supportedFields.add(Severity);
+ supportedFields.add(ThreadName);
+ supportedFields.add(SourceClassName);
+ supportedFields.add(SourceMethodName);
+ supportedFields.add(SourceSimpleClassName);
+ supportedFields.add(LoggerName);
+ supportedFields.add(NDC);
+ supportedFields.add(Server);
+
+ SUPPORTED_FIELDS = Collections.unmodifiableSet(supportedFields);
+
+ }
/**
* Create a new instance of the {@link WildFlyJsonFormatter}.
*/
public WildFlyJsonFormatter() {
- gelfMessageAssembler.addFields(LogMessageField.getDefaultMapping(Time, Severity, ThreadName, SourceClassName,
- SourceMethodName, SourceSimpleClassName, LoggerName, NDC, Server));
+
}
@Override
public String format(ExtLogRecord extLogRecord) {
+ if (!wasSetFieldsCalled) {
+ addFields(SUPPORTED_FIELDS);
+ }
+
GelfMessage gelfMessage = gelfMessageAssembler.createGelfMessage(new JBoss7JulLogEvent(extLogRecord));
- return gelfMessage.toJson("") + lineBreak;
+ return gelfMessage.toJson("") + getLineBreak();
+ }
+
+ public void setFields(String fieldSpec) {
+
+ String[] properties = fieldSpec.split(MULTI_VALUE_DELIMITTER);
+ List fields = new ArrayList();
+ for (String field : properties) {
+
+ LogMessageField.NamedLogField namedLogField = LogMessageField.NamedLogField.byName(field.trim());
+ if (namedLogField == null) {
+ throw new IllegalArgumentException("Cannot resolve field name '" + field
+ + "' to a field. Supported field names are: " + SUPPORTED_FIELDS);
+ }
+
+ if (!SUPPORTED_FIELDS.contains(namedLogField)) {
+ throw new IllegalArgumentException("Field '" + field + "' is not supported. Supported field names are: "
+ + SUPPORTED_FIELDS);
+ }
+
+ fields.add(namedLogField);
+ }
+
+ addFields(fields);
+
+ }
+
+ private void addFields(Collection fields) {
+ gelfMessageAssembler.addFields(LogMessageField.getDefaultMapping(fields
+ .toArray(new LogMessageField.NamedLogField[fields.size()])));
+
+ wasSetFieldsCalled = true;
}
public void setAdditionalFields(String fieldSpec) {
- String[] properties = fieldSpec.split(",");
+ String[] properties = fieldSpec.split(MULTI_VALUE_DELIMITTER);
for (String field : properties) {
final int index = field.indexOf('=');
@@ -106,7 +165,7 @@ public void setAdditionalFields(String fieldSpec) {
}
public void setMdcFields(String fieldSpec) {
- String[] fields = fieldSpec.split(",");
+ String[] fields = fieldSpec.split(MULTI_VALUE_DELIMITTER);
for (String field : fields) {
gelfMessageAssembler.addField(new MdcMessageField(field.trim(), field.trim()));
@@ -114,7 +173,7 @@ public void setMdcFields(String fieldSpec) {
}
public void setDynamicMdcFields(String fieldSpec) {
- String[] fields = fieldSpec.split(",");
+ String[] fields = fieldSpec.split(MULTI_VALUE_DELIMITTER);
for (String field : fields) {
gelfMessageAssembler.addField(new DynamicMdcMessageField(field.trim()));
diff --git a/src/site/markdown/examples/wildfly-json.md b/src/site/markdown/examples/wildfly-json.md
index 4922449fd..b9f7fe1d9 100644
--- a/src/site/markdown/examples/wildfly-json.md
+++ b/src/site/markdown/examples/wildfly-json.md
@@ -2,6 +2,8 @@ Settings
--------------
Following settings can be used:
+ * lineBreak (Optional): End of line, defaults to `\n`
+ * fields (Optional): Comma-separated list of log event fields that should be included in the JSON. Defaults to `Time, Severity, ThreadName, SourceClassName, SourceMethodName, SourceSimpleClassName, LoggerName, NDC`
* version (Optional): GELF Version 1.0 or 1.1, default 1.0
* originHost (Optional): Originating Hostname, default FQDN Hostname
* extractStackTrace (Optional): Post Stack-Trace to StackTrace field, default false
@@ -49,6 +51,7 @@ XML Configuration:
+
@@ -81,6 +84,7 @@ CLI Configuration:
/subsystem=logging/custom-formatter=JsonFormatter/:add(module=biz.paluch.logging,class=biz.paluch.logging.gelf.wildfly.WildFlyJsonFormatter,properties={ \
version="1.0", \
facility="java-test", \
+ fields="Time,Severity,ThreadName,SourceClassName,SourceMethodName,SourceSimpleClassName,LoggerName,NDC", \
extractStackTrace=true, \
filterStackTrace=true, \
mdcProfiling=true, \
diff --git a/src/test/java/biz/paluch/logging/gelf/wildfly/WildFlyGelfLogFormatterTest.java b/src/test/java/biz/paluch/logging/gelf/wildfly/WildFlyGelfLogFormatterTest.java
index 80ca4a50d..181d1f455 100644
--- a/src/test/java/biz/paluch/logging/gelf/wildfly/WildFlyGelfLogFormatterTest.java
+++ b/src/test/java/biz/paluch/logging/gelf/wildfly/WildFlyGelfLogFormatterTest.java
@@ -1,11 +1,7 @@
package biz.paluch.logging.gelf.wildfly;
import static org.hamcrest.CoreMatchers.containsString;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import java.io.StringWriter;
import java.util.Map;
@@ -122,6 +118,38 @@ public void testSimpleWithStringFormatSubstitution() throws Exception {
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testUnknownField() throws Exception {
+
+ WildFlyJsonFormatter formatter = new WildFlyJsonFormatter();
+ formatter.setFields("dummy");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotSupportedField() throws Exception {
+
+ WildFlyJsonFormatter formatter = new WildFlyJsonFormatter();
+ formatter.setFields("Marker");
+ }
+
+ @Test
+ public void testFields() throws Exception {
+
+ WildFlyJsonFormatter formatter = new WildFlyJsonFormatter();
+ formatter.setFields("Time,Severity,ThreadName,SourceSimpleClassName,NDC");
+
+ handler.setFormatter(formatter);
+ Logger logger = Logger.getLogger(getClass().getName());
+ logger.addHandler(handler);
+
+ logger.info(LOG_MESSAGE);
+
+ Map message = getMessage();
+
+ assertNotNull(message.get("SourceSimpleClassName"));
+ assertNull(message.get("LoggerName"));
+ }
+
@Test
public void testLineBreak() throws Exception {
@@ -138,7 +166,7 @@ public void testLineBreak() throws Exception {
}
@Test
- public void testFields() throws Exception {
+ public void testMdcFields() throws Exception {
WildFlyJsonFormatter formatter = new WildFlyJsonFormatter();
formatter.setOriginHost("myhost");