All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.request.logging.old.LogbackClassicRequestLogFactory Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha.2
Show newest version
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 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.CustomRequestLog;
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;
import java.util.Map;
import java.util.TimeZone;

/**
 * A factory for creating {@link RequestLog} instances using logback-classic.
 * 

* Configuration Parameters: *

* * * * * * * * * * * * * * * *
NameDefaultDescription
{@code timeZone}UTCThe 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 Collections.emptyMap(); } } @NotNull private TimeZone timeZone = TimeZone.getTimeZone("UTC"); @Valid @NotNull private List> appenders = Collections.singletonList( new ConsoleAppenderFactory() ); @JsonProperty public List> getAppenders() { return appenders; } @JsonProperty public void setAppenders(List> 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 CustomRequestLog(new DropwizardSlf4jRequestLogWriter(attachable), ClassicLogFormat.pattern(timeZone)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy