Forked from fedotxxl/LogbackAutoConfigLoggingFactory.java
Last active
February 6, 2017 06:23
-
-
Save mb-dbc-dk/fe4d94ebfd0410a6a83435dfc7cd3350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resetting the context, inhibits log lines from previous configuration (repeated WARN/ERROR with different format)