io.dropwizard.logging.AbstractAppenderFactory Maven / Gradle / Ivy
package io.dropwizard.logging;
import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AsyncAppenderBase;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.pattern.PatternLayoutBase;
import ch.qos.logback.core.spi.DeferredProcessingAware;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.filter.FilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.TimeZone;
/**
* A base implementation of {@link AppenderFactory}.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* {@code threshold}
* ALL
* The minimum event level the appender will handle.
*
*
* {@code logFormat}
* (none)
* An appender-specific log format.
*
*
* {@code timeZone}
* {@code UTC}
*
* The time zone to which event timestamps will be converted.
* Ignored if logFormat is supplied.
*
*
*
* {@code queueSize}
* {@link AsyncAppenderBase}
* The maximum capacity of the blocking queue.
*
*
* {@code includeCallerData}
* {@link AsyncAppenderBase}
*
* Whether to include caller data, required for line numbers.
* Beware, is considered expensive.
*
*
*
* {@code discardingThreshold}
* {@link AsyncAppenderBase}
*
* By default, when the blocking queue has 20% capacity remaining,
* it will drop events of level TRACE, DEBUG and INFO, keeping only
* events of level WARN and ERROR. To keep all events, set discardingThreshold to 0.
*
*
*
* {@code filterFactories}
* (none)
*
* A list of {@link FilterFactory filters} to apply to the appender, in order,
* after the {@code threshold}.
*
*
*
*/
public abstract class AbstractAppenderFactory implements AppenderFactory {
@NotNull
protected Level threshold = Level.ALL;
protected String logFormat;
@NotNull
protected TimeZone timeZone = TimeZone.getTimeZone("UTC");
@Min(1)
@Max(Integer.MAX_VALUE)
private int queueSize = AsyncAppenderBase.DEFAULT_QUEUE_SIZE;
private int discardingThreshold = -1;
private boolean includeCallerData = false;
private ImmutableList> filterFactories = ImmutableList.of();
@JsonProperty
public int getQueueSize() {
return queueSize;
}
@JsonProperty
public void setQueueSize(int queueSize) {
this.queueSize = queueSize;
}
@JsonProperty
public int getDiscardingThreshold() {
return discardingThreshold;
}
@JsonProperty
public void setDiscardingThreshold(int discardingThreshold) {
this.discardingThreshold = discardingThreshold;
}
@JsonProperty
public Level getThreshold() {
return threshold;
}
@JsonProperty
public void setThreshold(Level threshold) {
this.threshold = threshold;
}
@JsonProperty
public String getLogFormat() {
return logFormat;
}
@JsonProperty
public void setLogFormat(String logFormat) {
this.logFormat = logFormat;
}
@JsonProperty
public TimeZone getTimeZone() {
return timeZone;
}
@JsonProperty
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
@JsonProperty
public boolean isIncludeCallerData() {
return includeCallerData;
}
@JsonProperty
public void setIncludeCallerData(boolean includeCallerData) {
this.includeCallerData = includeCallerData;
}
@JsonProperty
public ImmutableList> getFilterFactories() {
return filterFactories;
}
@JsonProperty
public void setFilterFactories(List> appenders) {
this.filterFactories = ImmutableList.copyOf(appenders);
}
protected Appender wrapAsync(Appender appender, AsyncAppenderFactory asyncAppenderFactory) {
return wrapAsync(appender, asyncAppenderFactory, appender.getContext());
}
protected Appender wrapAsync(Appender appender, AsyncAppenderFactory asyncAppenderFactory, Context context) {
final AsyncAppenderBase asyncAppender = asyncAppenderFactory.build();
if (asyncAppender instanceof AsyncAppender) {
((AsyncAppender) asyncAppender).setIncludeCallerData(includeCallerData);
}
asyncAppender.setQueueSize(queueSize);
asyncAppender.setDiscardingThreshold(discardingThreshold);
asyncAppender.setContext(context);
asyncAppender.setName("async-" + appender.getName());
asyncAppender.addAppender(appender);
asyncAppender.start();
return asyncAppender;
}
protected PatternLayoutBase buildLayout(LoggerContext context, LayoutFactory layoutFactory) {
final PatternLayoutBase formatter = layoutFactory.build(context, timeZone);
if (!Strings.isNullOrEmpty(logFormat)) {
formatter.setPattern(logFormat);
}
formatter.start();
return formatter;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy