Skip to content

Instantly share code, notes, and snippets.

@filippomortari
Last active November 23, 2016 11:05
Show Gist options
  • Save filippomortari/dbe6a43c617ef50b7801d56c276a49a8 to your computer and use it in GitHub Desktop.
Save filippomortari/dbe6a43c617ef50b7801d56c276a49a8 to your computer and use it in GitHub Desktop.
a custom logger for Dropwizard apps to output correlation ids in request logs
package com.filippomortari;
import ch.qos.logback.classic.*;
import ch.qos.logback.classic.spi.*;
import ch.qos.logback.core.*;
import ch.qos.logback.core.spi.*;
import com.google.common.collect.ImmutableList;
import io.dropwizard.jetty.*;
import io.dropwizard.jetty.Slf4jRequestLog;
import io.dropwizard.logging.*;
import io.dropwizard.server.AbstractServerFactory;
import io.dropwizard.server.DefaultServerFactory;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.*;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Enumeration;
import java.util.TimeZone;
public class CustomRequestLogFactory extends RequestLogFactory {
private final ApplicationConfiguration applicationConfiguration;
@NotNull
private TimeZone timeZone = TimeZone.getTimeZone("UTC");
@Valid
@NotNull
private ImmutableList<AppenderFactory> appenders;
public CustomRequestLogFactory(ApplicationConfiguration configuration) {
this.applicationConfiguration = configuration;
this.appenders = ((AbstractServerFactory) applicationConfiguration.getServerFactory()).getRequestLogFactory().getAppenders();
}
private static class RequestLogLayout extends LayoutBase<ILoggingEvent> {
@Override
public String doLayout(ILoggingEvent event) {
return event.getFormattedMessage() + CoreConstants.LINE_SEPARATOR;
}
}
private static class CustomRequestLog extends Slf4jRequestLog {
private static Logger logger = (Logger) LoggerFactory.getLogger(CustomRequestLog.class);
private static ThreadLocal<StringBuilder> _buffers = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder(256);
}
};
public CustomRequestLog(AppenderAttachableImpl<ILoggingEvent> appenders,
TimeZone timeZone) {
super(appenders, timeZone);
this.setExtended(true);
}
@Override
protected void logExtended(Request request,
Response response,
StringBuilder b) throws IOException
{
super.logExtended(request,response, b);
// it violates the NCSA log standard, YMMV
b.append(" ["+response.getHeader("X-CORRELATION-ID")+"]");
}
}
public RequestLog build(String name) {
final Logger logger = (Logger) LoggerFactory.getLogger("http.request");
logger.setAdditive(false);
final LoggerContext context = logger.getLoggerContext();
final RequestLogLayout layout = new RequestLogLayout();
layout.start();
final AppenderAttachableImpl<ILoggingEvent> attachable =
new AppenderAttachableImpl<>();
for (AppenderFactory output : this.appenders) {
attachable.addAppender(output.build(context, name, layout));
}
return new CustomRequestLog(attachable, timeZone);
}
}
package com.filippomortari;
import com.yammer.tenacity.core.bundle.TenacityBundleBuilder;
import io.dropwizard.Application;
import io.dropwizard.server.DefaultServerFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import static java.lang.String.format;
public class DropwizardApplication extends Application<ApplicationConfiguration> {
public static void main(final String[] args) throws Exception {
new DropwizardApplication().run(args);
}
@Override
public String getName() {
return "bla-bla-bla";
}
@Override
public void initialize(final Bootstrap<ApplicationConfiguration> bootstrap) {
bootstrap.addBundle(new ViewBundle());
}
@Override
public void run(final ApplicationConfiguration configuration, final Environment environment) throws Exception {
((DefaultServerFactory) configuration.getServerFactory())
.setRequestLogFactory(new CustomRequestLogFactory(configuration));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment