Xorlev/dropwizard-gelf

Addon bundle for Dropwizard to support logging to a GELF-enabled server like Graylog2 or logstash

โ˜… 0Forks 0JavaGitHub โ†—Compare

Project website โ†—

README

Dropwizard GELF Bundle

Addon bundle for Dropwizard to support logging to a GELF-enabled server like Graylog2 or logstash using the GELF appender for Logback.

Build Status

Usage

dropwizard-gelf consists of two parts, a ConfiguredBundle and a Servlet Filter which can optionally be used to send HTTP request logs to a GELF-enabled server.

To enable the GelfLoggingBundle simply add the following code to your Service's initialize method:

@Override
public void initialize(Bootstrap<MyServiceConfiguration> bootstrap) {
    bootstrap.addBundle(new GelfLoggingBundle<MyServiceConfiguration>() {
            @Override
            public GelfConfiguration getConfiguration(MyServiceConfiguration configuration) {
                return configuration.getGelf();
            }
        });
}

You also need to add a field for GelfConfiguration to your own Configuration class.

In order to log HTTP requests being sent to your service you need to add the GelfLoggingFilter in the run method of your service:

@Override
public void run(MyServiceConfiguration configuration, Environment environment) {
    environment.addFilter(new GelfLoggingFilter(), "/*");
}

The servlet filter adds a lot of data to the Mapped Diagnostic Context (MDC) which can be used as additional fields, e. g. remoteAddress, requestUri, or responseStatus.

Configuration

The Logback GELF appender can be configured using the provided GelfConfiguration class which basically mirrors the appender configuration outlined in logback-gelf README.

Your YAML configuration could include the following snippet to configure the GelfLoggingBundle:

gelf:
  enabled: true
  facility: MyService
  # threshold: ALL
  # host: localhost
  # port: 12201
  # useLoggerName: true
  # useThreadName: true
  # serverVersion: 0.9.6
  # chunkThreshold: 1000
  # messagePattern: %m%rEx
  additionalFields:
    remoteAddress: _remoteAddress
    httpMethod: _httpMethod
    requestUri: _requestUri
    responseStatus: _responseStatus
    responseTimeNanos: _responseTime

Drawbacks

Being implemented as a ConfiguredBundle and a Servlet Filter (for logging HTTP requests) dropwizard-gelf basically starts too late to catch all the startup log messages (e. g. from other bundles or before the GelfLoggingBundle has been started) and duplicates a lot of functionality from Dropwizard's RequestLogHandlerFactory.

On the long run the clean solution would be to add pluggable log handlers to Dropwizard in a way that won't break the current abstraction.

Maven Artifacts

This project is available on Maven Central. To add it to your project simply add the following dependencies to your pom.xml:

<dependency>
  <groupId>com.smarchive.dropwizard</groupId>
  <artifactId>dropwizard-gelf</artifactId>
  <version>0.1</version>
</dependency>

Acknowledgements

Thanks to Nick Telford for his initial version of the GraylogBundle.

License

Copyright (c) 2012 smarchive GmbH

This library is licensed under the Apache License, Version 2.0.

See http://www.apache.org/licenses/LICENSE-2.0.html or the LICENSE file in this repository for the full license text.

Support

Please log tickets and issues at our project site.

Issues