Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mb-dbc-dk/fe4d94ebfd0410a6a83435dfc7cd3350 to your computer and use it in GitHub Desktop.
Save mb-dbc-dk/fe4d94ebfd0410a6a83435dfc7cd3350 to your computer and use it in GitHub Desktop.
package com.devadmin.utils.dropwizard;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.dropwizard.logging.LoggingFactory;
import io.dropwizard.logging.LoggingUtil;
/**
* https://github.com/dropwizard/dropwizard/issues/1567
* Override getLoggingFactory for your configuration
*/
public class LogbackAutoConfigLoggingFactory implements LoggingFactory {
@JsonIgnore
private final LoggerContext loggerContext;
@JsonIgnore
private final ContextInitializer contextInitializer;
public LogbackAutoConfigLoggingFactory() {
this.loggerContext = LoggingUtil.getLoggerContext();
this.contextInitializer = new ContextInitializer(loggerContext);
}
@Override
public void configure(MetricRegistry metricRegistry, String name) {
try {
loggerContext.reset();
contextInitializer.autoConfig();
} catch (JoranException e) {
throw new RuntimeException(e);
}
}
@Override
public void stop() {
loggerContext.stop();
}
}
@mb-dbc-dk
Copy link
Author

Resetting the context, inhibits log lines from previous configuration (repeated WARN/ERROR with different format)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment