io.dropwizard.request.logging.old.LogbackClassicRequestLogFactory Maven / Gradle / Ivy
package io.dropwizard.request.logging.old;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.pattern.PatternLayoutBase;
import ch.qos.logback.core.spi.AppenderAttachableImpl;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.dropwizard.logging.AppenderFactory;
import io.dropwizard.logging.ConsoleAppenderFactory;
import io.dropwizard.logging.async.AsyncAppenderFactory;
import io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory;
import io.dropwizard.logging.filter.LevelFilterFactory;
import io.dropwizard.logging.filter.NullLevelFilterFactory;
import io.dropwizard.logging.layout.LayoutFactory;
import io.dropwizard.request.logging.RequestLogFactory;
import org.eclipse.jetty.server.RequestLog;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.TimeZone;
/**
* A factory for creating {@link RequestLog} instances using logback-classic.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* {@code timeZone}
* UTC
* The time zone to which request timestamps will be converted.
*
*
* {@code appenders}
* a default {@link ConsoleAppenderFactory console} appender
*
* The set of {@link AppenderFactory appenders} to which requests will be logged.
*
*
*
*/
@JsonTypeName("classic")
public class LogbackClassicRequestLogFactory implements RequestLogFactory {
private static class RequestLogLayout extends PatternLayoutBase {
private RequestLogLayout(Context context) {
super();
setContext(context);
}
@Override
public String doLayout(ILoggingEvent event) {
return event.getFormattedMessage() + CoreConstants.LINE_SEPARATOR;
}
@Override
public Map getDefaultConverterMap() {
return ImmutableMap.of();
}
}
@NotNull
private TimeZone timeZone = TimeZone.getTimeZone("UTC");
@Valid
@NotNull
private ImmutableList> appenders = ImmutableList.of(
new ConsoleAppenderFactory()
);
@JsonProperty
public ImmutableList> getAppenders() {
return appenders;
}
@JsonProperty
public void setAppenders(ImmutableList> appenders) {
this.appenders = appenders;
}
@JsonProperty
public TimeZone getTimeZone() {
return timeZone;
}
@JsonProperty
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
@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 LevelFilterFactory levelFilterFactory = new NullLevelFilterFactory<>();
final AsyncAppenderFactory asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
final LayoutFactory layoutFactory = (c, tz) -> new RequestLogLayout(c);
final AppenderAttachableImpl attachable = new AppenderAttachableImpl<>();
for (AppenderFactory appender : appenders) {
attachable.addAppender(appender.build(context, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
}
return new DropwizardSlf4jRequestLog(attachable, timeZone);
}
}