Search in sources :

Example 1 with ShutdownCallbackRegistry

use of org.apache.logging.log4j.core.util.ShutdownCallbackRegistry 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)

Example 2 with ShutdownCallbackRegistry

use of org.apache.logging.log4j.core.util.ShutdownCallbackRegistry in project logging-log4j2 by apache.

the class LoggerContext method setUpShutdownHook.

private void setUpShutdownHook() {
    if (shutdownCallback == null) {
        final LoggerContextFactory factory = LogManager.getFactory();
        if (factory instanceof ShutdownCallbackRegistry) {
            LOGGER.debug(SHUTDOWN_HOOK_MARKER, "Shutdown hook enabled. Registering a new one.");
            try {
                final long shutdownTimeoutMillis = this.configuration.getShutdownTimeoutMillis();
                this.shutdownCallback = ((ShutdownCallbackRegistry) factory).addShutdownCallback(new Runnable() {

                    @Override
                    public void run() {
                        @SuppressWarnings("resource") final LoggerContext context = LoggerContext.this;
                        LOGGER.debug(SHUTDOWN_HOOK_MARKER, "Stopping LoggerContext[name={}, {}]", context.getName(), context);
                        context.stop(shutdownTimeoutMillis, TimeUnit.MILLISECONDS);
                    }

                    @Override
                    public String toString() {
                        return "Shutdown callback for LoggerContext[name=" + LoggerContext.this.getName() + ']';
                    }
                });
            } catch (final IllegalStateException e) {
                throw new IllegalStateException("Unable to register Log4j shutdown hook because JVM is shutting down.", e);
            } catch (final SecurityException e) {
                LOGGER.error(SHUTDOWN_HOOK_MARKER, "Unable to register shutdown hook due to security restrictions", e);
            }
        }
    }
}
Also used : LoggerContextFactory(org.apache.logging.log4j.spi.LoggerContextFactory) ShutdownCallbackRegistry(org.apache.logging.log4j.core.util.ShutdownCallbackRegistry)

Aggregations

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