biz.paluch.logging.gelf.wildfly.WildFlyJsonFormatter Maven / Gradle / Ivy
package biz.paluch.logging.gelf.wildfly;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.LoggerName;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.NDC;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Server;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Severity;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceClassName;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceMethodName;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.SourceSimpleClassName;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.ThreadName;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.Time;
import biz.paluch.logging.gelf.intern.ConfigurationSupport;
import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;
import biz.paluch.logging.gelf.LogMessageField;
import biz.paluch.logging.gelf.MdcGelfMessageAssembler;
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:
*
*
{
"NDC": "ndc message",
"timestamp": "1439319236.722",
"SourceClassName": "biz.paluch.logging.gelf.wildfly.WildFlyGelfLogFormatterTest",
"SourceMethodName": "testDefaults",
"level": "6",
"SourceSimpleClassName": "WildFlyGelfLogFormatterTest",
"facility": "logstash-gelf",
"full_message": "foo bar test log message",
"short_message": "foo bar test log message",
"MySeverity": "INFO",
"LoggerName": "biz.paluch.logging.gelf.wildfly.WildFlyGelfLogFormatterTest",
"Thread": "main",
"MyTime": "2015-08-11 20:53:56,0722"
}
*
*
* 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
* - mdcProfiling (Optional): Perform Profiling (Call-Duration) based on MDC Data. See MDC
* Profiling, default false
* - facility (Optional): Name of the Facility, default gelf-java
* - additionalFields(number) (Optional): Post additional fields. Eg. fieldName=Value,field2=value2
* - additionalFieldTypes (Optional): Type specification for additional and MDC fields. Supported types: String, long, Long,
* double, Double and discover (default if not specified, discover field type on parseability). Eg. field=String,field2=double
* - mdcFields (Optional): Post additional fields, pull Values from MDC. Name of the Fields are comma-separated
* Application,Version,SomeOtherFieldName
* - dynamicMdcFields (Optional): Dynamic MDC Fields allows you to extract MDC values based on one or more regular
* expressions. Multiple regex are comma-separated. The name of the MDC entry is used as GELF field name. mdc.*,[mdc|MDC]fields
* - includeFullMdc (Optional): Include all fields from the MDC, default false
*
* MDC Profiling
*
* MDC Profiling allows to calculate the runtime from request start up to the time until the log message was generated. You must
* set one value in the MDC:
*
* - profiling.requestStart.millis: Time Millis of the Request-Start (Long or String)
*
*
* Two values are set by the formatter:
*
*
* - profiling.requestEnd: End-Time of the Request-End in Date.toString-representation
* - profiling.requestDuration: Duration of the request (e.g. 205ms, 16sec)
*
*
* @author Mark Paluch
*/
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() {
}
@Override
public String format(ExtLogRecord extLogRecord) {
if (!wasSetFieldsCalled) {
addFields(SUPPORTED_FIELDS);
}
GelfMessage gelfMessage = gelfMessageAssembler.createGelfMessage(new JBoss7JulLogEvent(extLogRecord));
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 spec) {
ConfigurationSupport.setAdditionalFields(spec, gelfMessageAssembler);
}
public void setAdditionalFieldTypes(String spec) {
ConfigurationSupport.setAdditionalFieldTypes(spec, gelfMessageAssembler);
}
public void setMdcFields(String spec) {
ConfigurationSupport.setMdcFields(spec, gelfMessageAssembler);
}
public void setDynamicMdcFields(String spec) {
ConfigurationSupport.setDynamicMdcFields(spec, gelfMessageAssembler);
}
public boolean isMdcProfiling() {
return gelfMessageAssembler.isMdcProfiling();
}
public void setMdcProfiling(boolean mdcProfiling) {
gelfMessageAssembler.setMdcProfiling(mdcProfiling);
}
public boolean isIncludeFullMdc() {
return gelfMessageAssembler.isIncludeFullMdc();
}
public void setIncludeFullMdc(boolean includeFullMdc) {
gelfMessageAssembler.setIncludeFullMdc(includeFullMdc);
}
public String getOriginHost() {
return gelfMessageAssembler.getOriginHost();
}
public void setOriginHost(String originHost) {
gelfMessageAssembler.setOriginHost(originHost);
}
public String getFacility() {
return gelfMessageAssembler.getFacility();
}
public void setFacility(String facility) {
gelfMessageAssembler.setFacility(facility);
}
public boolean isExtractStackTrace() {
return gelfMessageAssembler.isExtractStackTrace();
}
public void setExtractStackTrace(boolean extractStacktrace) {
gelfMessageAssembler.setExtractStackTrace(extractStacktrace);
}
public boolean isFilterStackTrace() {
return gelfMessageAssembler.isFilterStackTrace();
}
public void setFilterStackTrace(boolean filterStackTrace) {
gelfMessageAssembler.setFilterStackTrace(filterStackTrace);
}
public String getTimestampPattern() {
return gelfMessageAssembler.getTimestampPattern();
}
public void setTimestampPattern(String timestampPattern) {
gelfMessageAssembler.setTimestampPattern(timestampPattern);
}
public String getVersion() {
return gelfMessageAssembler.getVersion();
}
public void setVersion(String version) {
gelfMessageAssembler.setVersion(version);
}
public String getLineBreak() {
return lineBreak;
}
public void setLineBreak(String lineBreak) {
this.lineBreak = lineBreak;
}
}