Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Allow to specify log event fields #52
Browse files Browse the repository at this point in the history
Motivation: Users want to remove specific fields/define which fields should be containied within the JSON result.

This change allows to specify a comma-separated list of log fields, defaults to "Time, Severity, ThreadName, SourceClassName, SourceMethodName, SourceSimpleClassName, LoggerName, NDC" if the property is not set.
  • Loading branch information
mp911de committed Aug 12, 2015
1 parent 042e563 commit 923f228
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -46,6 +48,8 @@
* Following parameters are supported/needed:
* <ul>
* <li>lineBreak (Optional): End of line, defaults to {@code \n}</li>
* <li>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}</li>
* <li>originHost (Optional): Originating Hostname, default FQDN Hostname</li>
* <li>extractStacktrace (Optional): Post Stack-Trace to StackTrace field, default false</li>
* <li>filterStackTrace (Optional): Perform Stack-Trace filtering (true/false), default false</li>
Expand Down Expand Up @@ -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<LogMessageField.NamedLogField> SUPPORTED_FIELDS;

static {
Set<LogMessageField.NamedLogField> supportedFields = new LinkedHashSet<LogMessageField.NamedLogField>();

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<LogMessageField.NamedLogField> fields = new ArrayList<LogMessageField.NamedLogField>();
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<LogMessageField.NamedLogField> 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('=');
Expand All @@ -106,15 +165,15 @@ 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()));
}
}

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()));
Expand Down
4 changes: 4 additions & 0 deletions src/site/markdown/examples/wildfly-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +51,7 @@ XML Configuration:
<properties>
<property name="version" value="1.0" />
<property name="facility" value="java-test" />
<property name="fields" value="Time,Severity,ThreadName,SourceClassName,SourceMethodName,SourceSimpleClassName,LoggerName,NDC" />
<property name="extractStackTrace" value="true" />
<property name="filterStackTrace" value="true" />
<property name="mdcProfiling" value="true" />
Expand Down Expand Up @@ -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, \
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, Object> message = getMessage();

assertNotNull(message.get("SourceSimpleClassName"));
assertNull(message.get("LoggerName"));
}

@Test
public void testLineBreak() throws Exception {

Expand All @@ -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");
Expand Down

0 comments on commit 923f228

Please sign in to comment.