biz.paluch.logging.gelf.logback.GelfLogbackAppender Maven / Gradle / Ivy
package biz.paluch.logging.gelf.logback;
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.*;
import java.util.Collections;
import biz.paluch.logging.RuntimeContainer;
import biz.paluch.logging.gelf.LogMessageField;
import biz.paluch.logging.gelf.MdcGelfMessageAssembler;
import biz.paluch.logging.gelf.intern.*;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
/**
* Logging-Handler for GELF (Graylog Extended Logging Format). This Logback Handler creates GELF Messages and posts them using
* UDP (default) or TCP. Following parameters are supported/needed:
*
* - host (Mandatory): Hostname/IP-Address of the Logstash Host
*
* - (the host) for UDP, e.g. 127.0.0.1 or some.host.com
* - See docs for more details
*
*
* - port (Optional): Port, default 12201
* - 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 (true/false/throwable reference [0 = throwable, 1 = throwable.cause, -1 = root cause]), 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
* - filter (Optional): logback filter (incl. log level)
* - additionalFields(number) (Optional): Post additional fields. Eg.
* .GelfLogHandler.additionalFields=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
* mdcFields=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.
* - 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 Log Appender:
*
*
* - profiling.requestEnd: End-Time of the Request-End in Date.toString-representation
* - profiling.requestDuration: Duration of the request (e.g. 205ms, 16sec)
*
*
* The {@link #append(ILoggingEvent)} method is thread-safe and may be called by different threads at any time.
*
* @author Tobias Kaefer
* @since 2013-10-08
*/
public class GelfLogbackAppender extends AppenderBase implements ErrorReporter {
protected GelfSender gelfSender;
protected MdcGelfMessageAssembler gelfMessageAssembler;
private final ErrorReporter errorReporter = new MessagePostprocessingErrorReporter(this);
public GelfLogbackAppender() {
super();
gelfMessageAssembler = new MdcGelfMessageAssembler();
gelfMessageAssembler.addFields(LogMessageField.getDefaultMapping(Time, Severity, ThreadName, SourceClassName,
SourceMethodName, SourceLineNumber, SourceSimpleClassName, LoggerName, Marker));
}
@Override
protected void append(ILoggingEvent event) {
if (event == null) {
return;
}
try {
GelfMessage message = createGelfMessage(event);
if (!message.isValid()) {
reportError("GELF Message is invalid: " + message.toJson(), null);
return;
}
if (null == gelfSender || !gelfSender.sendMessage(message)) {
reportError("Could not send GELF message", null);
}
} catch (Exception e) {
reportError("Could not send GELF message: " + e.getMessage(), e);
}
}
@Override
public void start() {
if (null == gelfSender) {
RuntimeContainer.initialize(errorReporter);
gelfSender = createGelfSender();
}
super.start();
}
@Override
public void stop() {
if(null != gelfSender) {
Closer.close(gelfSender);
gelfSender = null;
}
super.stop();
}
protected GelfSender createGelfSender() {
return GelfSenderFactory.createSender(gelfMessageAssembler, errorReporter, Collections.EMPTY_MAP);
}
@Override
public void reportError(String message, Exception exception) {
addError(message, exception);
}
protected GelfMessage createGelfMessage(final ILoggingEvent loggingEvent) {
return gelfMessageAssembler.createGelfMessage(new LogbackLogEvent(loggingEvent));
}
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 String getGraylogHost() {
return gelfMessageAssembler.getHost();
}
public void setGraylogHost(String graylogHost) {
gelfMessageAssembler.setHost(graylogHost);
}
public String getOriginHost() {
return gelfMessageAssembler.getOriginHost();
}
public void setOriginHost(String originHost) {
gelfMessageAssembler.setOriginHost(originHost);
}
public int getGraylogPort() {
return gelfMessageAssembler.getPort();
}
public void setGraylogPort(int graylogPort) {
gelfMessageAssembler.setPort(graylogPort);
}
public String getHost() {
return gelfMessageAssembler.getHost();
}
public void setHost(String host) {
gelfMessageAssembler.setHost(host);
}
public int getPort() {
return gelfMessageAssembler.getPort();
}
public void setPort(int port) {
gelfMessageAssembler.setPort(port);
}
public String getFacility() {
return gelfMessageAssembler.getFacility();
}
public void setFacility(String facility) {
gelfMessageAssembler.setFacility(facility);
}
public String getExtractStackTrace() {
return gelfMessageAssembler.getExtractStackTrace();
}
public void setExtractStackTrace(String extractStacktrace) {
gelfMessageAssembler.setExtractStackTrace(extractStacktrace);
}
public boolean isFilterStackTrace() {
return gelfMessageAssembler.isFilterStackTrace();
}
public void setFilterStackTrace(boolean filterStackTrace) {
gelfMessageAssembler.setFilterStackTrace(filterStackTrace);
}
public boolean isMdcProfiling() {
return gelfMessageAssembler.isMdcProfiling();
}
public void setMdcProfiling(boolean mdcProfiling) {
gelfMessageAssembler.setMdcProfiling(mdcProfiling);
}
public String getTimestampPattern() {
return gelfMessageAssembler.getTimestampPattern();
}
public void setTimestampPattern(String timestampPattern) {
gelfMessageAssembler.setTimestampPattern(timestampPattern);
}
public int getMaximumMessageSize() {
return gelfMessageAssembler.getMaximumMessageSize();
}
public void setMaximumMessageSize(int maximumMessageSize) {
gelfMessageAssembler.setMaximumMessageSize(maximumMessageSize);
}
public boolean isIncludeFullMdc() {
return gelfMessageAssembler.isIncludeFullMdc();
}
public void setIncludeFullMdc(boolean includeFullMdc) {
gelfMessageAssembler.setIncludeFullMdc(includeFullMdc);
}
public String getVersion() {
return gelfMessageAssembler.getVersion();
}
public void setVersion(String version) {
gelfMessageAssembler.setVersion(version);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy