net.logstash.log4j.JSONEventLayoutV0 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsonevent-layout Show documentation
Show all versions of jsonevent-layout Show documentation
Log4j pattern layout that conforms to the logstash json_event format
package net.logstash.log4j;
import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.logstash.log4j.data.HostData;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
public class JSONEventLayoutV0 extends Layout {
private static final ObjectMapper JSON = new ObjectMapper()
.configure(Feature.ESCAPE_NON_ASCII, true);
private boolean locationInfo = false;
private String tags;
private boolean ignoreThrowable = false;
private boolean activeIgnoreThrowable = ignoreThrowable;
private String hostname = new HostData().getHostName();
private String threadName;
private long timestamp;
private String ndc;
private Map mdc;
private LocationInfo info;
private HashMap fieldData;
private HashMap exceptionInformation;
private Map logstashEvent;
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", UTC);
public static String dateFormat(long timestamp) {
return ISO_DATETIME_TIME_ZONE_FORMAT_WITH_MILLIS.format(timestamp);
}
/**
* For backwards compatibility, the default is to generate location information
* in the log messages.
*/
public JSONEventLayoutV0() {
this(true);
}
/**
* Creates a layout that optionally inserts location information into log messages.
*
* @param locationInfo whether or not to include location information in the log messages.
*/
public JSONEventLayoutV0(boolean locationInfo) {
this.locationInfo = locationInfo;
}
public String format(LoggingEvent loggingEvent) {
threadName = loggingEvent.getThreadName();
timestamp = loggingEvent.getTimeStamp();
fieldData = new HashMap();
exceptionInformation = new HashMap();
mdc = loggingEvent.getProperties();
ndc = loggingEvent.getNDC();
logstashEvent = new HashMap();
logstashEvent.put("@source_host", hostname);
logstashEvent.put("@message", loggingEvent.getRenderedMessage());
logstashEvent.put("@timestamp", dateFormat(timestamp));
if (loggingEvent.getThrowableInformation() != null) {
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
}
if (throwableInformation.getThrowable().getMessage() != null) {
exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
}
if (throwableInformation.getThrowableStrRep() != null) {
String stackTrace = StringUtils.join(throwableInformation.getThrowableStrRep(), "\n");
exceptionInformation.put("stacktrace", stackTrace);
}
addFieldData("exception", exceptionInformation);
}
if (locationInfo) {
info = loggingEvent.getLocationInformation();
addFieldData("file", info.getFileName());
addFieldData("line_number", info.getLineNumber());
addFieldData("class", info.getClassName());
addFieldData("method", info.getMethodName());
}
addFieldData("loggerName", loggingEvent.getLoggerName());
addFieldData("mdc", mdc);
addFieldData("ndc", ndc);
addFieldData("level", loggingEvent.getLevel().toString());
addFieldData("threadName", threadName);
logstashEvent.put("@fields", fieldData);
return safeString(logstashEvent) + "\n";
}
private String safeString(final Map map) {
try {
return JSON.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace(System.err);
return "{}";
}
}
public boolean ignoresThrowable() {
return ignoreThrowable;
}
/**
* Query whether log messages include location information.
*
* @return true if location information is included in log messages, false otherwise.
*/
public boolean getLocationInfo() {
return locationInfo;
}
/**
* Set whether log messages should include location information.
*
* @param locationInfo true if location information should be included, false otherwise.
*/
public void setLocationInfo(boolean locationInfo) {
this.locationInfo = locationInfo;
}
public void activateOptions() {
activeIgnoreThrowable = ignoreThrowable;
}
private void addFieldData(String keyname, Object keyval) {
if (null != keyval) {
fieldData.put(keyname, keyval);
}
}
}