Search in sources :

Example 1 with Log4jShutdown

use of io.druid.common.config.Log4jShutdown in project druid by druid-io.

the class Log4jShutterDownerModule method configure.

@Override
public void configure(Binder binder) {
    try {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = getClass().getClassLoader();
        }
        // Reflection to try and allow non Log4j2 stuff to run. This acts as a gateway to stop errors in the next few lines
        // In log4j api
        final Class<?> logManagerClazz = Class.forName("org.apache.logging.log4j.LogManager", false, loader);
        // In log4j core
        final Class<?> callbackRegistryClazz = Class.forName("org.apache.logging.log4j.core.util.ShutdownCallbackRegistry", false, loader);
        final LoggerContextFactory contextFactory = LogManager.getFactory();
        if (!(contextFactory instanceof Log4jContextFactory)) {
            log.warn("Expected [%s] found [%s]. Unknown class for context factory. Not logging shutdown", Log4jContextFactory.class.getCanonicalName(), contextFactory.getClass().getCanonicalName());
            return;
        }
        final ShutdownCallbackRegistry registry = ((Log4jContextFactory) contextFactory).getShutdownCallbackRegistry();
        if (!(registry instanceof Log4jShutdown)) {
            log.warn("Shutdown callback registry expected class [%s] found [%s]. Skipping shutdown registry", Log4jShutdown.class.getCanonicalName(), registry.getClass().getCanonicalName());
            return;
        }
        binder.bind(Log4jShutdown.class).toInstance((Log4jShutdown) registry);
        binder.bind(Key.get(Log4jShutterDowner.class, Names.named("ForTheEagerness"))).to(Log4jShutterDowner.class).asEagerSingleton();
    } catch (ClassNotFoundException | ClassCastException | LinkageError e) {
        log.warn(e, "Not registering log4j shutdown hooks. Not using log4j?");
    }
}
Also used : Log4jContextFactory(org.apache.logging.log4j.core.impl.Log4jContextFactory) Log4jShutdown(io.druid.common.config.Log4jShutdown) LoggerContextFactory(org.apache.logging.log4j.spi.LoggerContextFactory) ShutdownCallbackRegistry(org.apache.logging.log4j.core.util.ShutdownCallbackRegistry)

Aggregations

Log4jShutdown (io.druid.common.config.Log4jShutdown)1 Log4jContextFactory (org.apache.logging.log4j.core.impl.Log4jContextFactory)1 ShutdownCallbackRegistry (org.apache.logging.log4j.core.util.ShutdownCallbackRegistry)1 LoggerContextFactory (org.apache.logging.log4j.spi.LoggerContextFactory)1