Search in sources :

Example 1 with LoggerContextFactory

use of org.apache.logging.log4j.spi.LoggerContextFactory 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 LoggerContextFactory

use of org.apache.logging.log4j.spi.LoggerContextFactory 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)

Example 3 with LoggerContextFactory

use of org.apache.logging.log4j.spi.LoggerContextFactory in project logging-log4j2 by apache.

the class Log4jWebInitializerImpl method initializeJndi.

private void initializeJndi(final String location) {
    final URI configLocation = getConfigURI(location);
    if (this.name == null) {
        throw new IllegalStateException("A log4jContextName context parameter is required");
    }
    LoggerContext context;
    final LoggerContextFactory factory = LogManager.getFactory();
    if (factory instanceof Log4jContextFactory) {
        final ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
        if (selector instanceof NamedContextSelector) {
            this.namedContextSelector = (NamedContextSelector) selector;
            context = this.namedContextSelector.locateContext(this.name, this.servletContext, configLocation);
            ContextAnchor.THREAD_CONTEXT.set(context);
            if (context.isInitialized()) {
                context.start();
            }
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            LOGGER.warn("Potential problem: Selector is not an instance of NamedContextSelector.");
            return;
        }
    } else {
        LOGGER.warn("Potential problem: LoggerContextFactory is not an instance of Log4jContextFactory.");
        return;
    }
    this.loggerContext = context;
    LOGGER.debug("Created logger context for [{}] using [{}].", this.name, context.getClass().getClassLoader());
}
Also used : Log4jContextFactory(org.apache.logging.log4j.core.impl.Log4jContextFactory) ContextSelector(org.apache.logging.log4j.core.selector.ContextSelector) NamedContextSelector(org.apache.logging.log4j.core.selector.NamedContextSelector) LoggerContextFactory(org.apache.logging.log4j.spi.LoggerContextFactory) URI(java.net.URI) LoggerContext(org.apache.logging.log4j.core.LoggerContext) AsyncLoggerContext(org.apache.logging.log4j.core.async.AsyncLoggerContext) NamedContextSelector(org.apache.logging.log4j.core.selector.NamedContextSelector)

Aggregations

LoggerContextFactory (org.apache.logging.log4j.spi.LoggerContextFactory)3 Log4jContextFactory (org.apache.logging.log4j.core.impl.Log4jContextFactory)2 ShutdownCallbackRegistry (org.apache.logging.log4j.core.util.ShutdownCallbackRegistry)2 Log4jShutdown (io.druid.common.config.Log4jShutdown)1 URI (java.net.URI)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 AsyncLoggerContext (org.apache.logging.log4j.core.async.AsyncLoggerContext)1 ContextSelector (org.apache.logging.log4j.core.selector.ContextSelector)1 NamedContextSelector (org.apache.logging.log4j.core.selector.NamedContextSelector)1