io.dropwizard.request.logging.LogbackAccessRequestLogFactory Maven / Gradle / Ivy
package io.dropwizard.request.logging;
import ch.qos.logback.access.spi.IAccessEvent;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.AppenderFactory;
import io.dropwizard.logging.ConsoleAppenderFactory;
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.filter.LevelFilterFactory;
import io.dropwizard.logging.filter.NullLevelFilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;
import io.dropwizard.request.logging.async.AsyncAccessEventAppenderFactory;
import io.dropwizard.request.logging.layout.LogbackAccessRequestLayoutFactory;
import org.eclipse.jetty.server.RequestLog;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
/**
* A factory for creating {@link LogbackAccessRequestLog} instances.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* {@code appenders}
* a default {@link ConsoleAppenderFactory console} appender
* The set of {@link AppenderFactory appenders} to which requests will be logged.
*
*
*/
@JsonTypeName("logback-access")
public class LogbackAccessRequestLogFactory implements RequestLogFactory {
@Valid
@NotNull
private List> appenders = Collections.singletonList(
new ConsoleAppenderFactory<>());
@JsonProperty
public List> getAppenders() {
return appenders;
}
@JsonProperty
public void setAppenders(List> appenders) {
this.appenders = appenders;
}
@JsonIgnore
@Override
public boolean isEnabled() {
return !appenders.isEmpty();
}
@Override
public RequestLog build(String name) {
final Logger logger = (Logger) LoggerFactory.getLogger("http.request");
logger.setAdditive(false);
final LoggerContext context = logger.getLoggerContext();
final LogbackAccessRequestLog requestLog = new LogbackAccessRequestLog();
final LevelFilterFactory levelFilterFactory = new NullLevelFilterFactory<>();
final AsyncAppenderFactory asyncAppenderFactory = new AsyncAccessEventAppenderFactory();
final LayoutFactory layoutFactory = new LogbackAccessRequestLayoutFactory();
for (AppenderFactory output : appenders) {
requestLog.addAppender(output.build(context, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
}
return requestLog;
}
}