Search code examples
javadropwizardgraylog

Dropwizard GELF logging appender


I'm using this addon bundle for dropwizard to log messages to a Graylog server: https://github.com/gini/dropwizard-gelf

It worked out of the box simply by adding it to the pom.xml and configuring it in the config.yml of my dropwizard server. Great stuff!

The only problem is, if my Graylog server is not available during startup for some reason, the GelfAppenderFactory throws a RuntimeException and dropwizard exits. My webserver never starts just because the logging server is unavailable. Not good.

Any ideas how I can get around it? My current approach would be to copy&paste the GelfAppenderFactory to my own code and wrap the crucial part in a try/catch block. That feels rather crude... so any help is much appreciated.


Solution

  • To achieve what I wanted I gave up trying to use dropwizard-gelf. Which is probably a good idea anyway, because it uses an outdated version of a discontinued plugin (https://github.com/Moocar/logback-gelf).

    Instead I reset dropwizard's logging configuration and reverted to loading a logback.xml file from the classpath like this:

    void reset() {
      ILoggerFactory factory = LoggerFactory.getILoggerFactory();
      LoggerContext context = (LoggerContext)factory;
      context.reset();
      ContextInitializer initializer = new ContextInitializer(context);
      initializer.autoConfig();
    }
    

    Unfortunately Dropwizard does not provide any standard way to re-enable Logback's standard ways (see https://github.com/dropwizard/dropwizard/pull/567)

    However, now I am free to use the (actively developed) plugin logstash-gelf (https://github.com/mp911de/logstash-gelf) with a Logback configuration.

    Thanks @pandaadb for all your help. In the end digging into Dropwizard's inner workings was just too much hassle for me.