Search in sources :

Example 1 with Log4jContextFactory

use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project hive by apache.

the class TestHiveAsyncLogging method testAsyncLoggingInitialization.

// this test requires disruptor jar in classpath
@Test
public void testAsyncLoggingInitialization() throws Exception {
    HiveConf conf = new HiveConf();
    conf.setBoolVar(ConfVars.HIVE_ASYNC_LOG_ENABLED, false);
    LogUtils.initHiveLog4jCommon(conf, ConfVars.HIVE_LOG4J_FILE);
    Log4jContextFactory log4jContextFactory = (Log4jContextFactory) LogManager.getFactory();
    ContextSelector contextSelector = log4jContextFactory.getSelector();
    assertTrue(contextSelector instanceof ClassLoaderContextSelector);
    conf.setBoolVar(ConfVars.HIVE_ASYNC_LOG_ENABLED, true);
    LogUtils.initHiveLog4jCommon(conf, ConfVars.HIVE_LOG4J_FILE);
    log4jContextFactory = (Log4jContextFactory) LogManager.getFactory();
    contextSelector = log4jContextFactory.getSelector();
    assertTrue(contextSelector instanceof AsyncLoggerContextSelector);
}
Also used : Log4jContextFactory(org.apache.logging.log4j.core.impl.Log4jContextFactory) AsyncLoggerContextSelector(org.apache.logging.log4j.core.async.AsyncLoggerContextSelector) ContextSelector(org.apache.logging.log4j.core.selector.ContextSelector) ClassLoaderContextSelector(org.apache.logging.log4j.core.selector.ClassLoaderContextSelector) ClassLoaderContextSelector(org.apache.logging.log4j.core.selector.ClassLoaderContextSelector) AsyncLoggerContextSelector(org.apache.logging.log4j.core.async.AsyncLoggerContextSelector) Test(org.junit.Test)

Example 2 with Log4jContextFactory

use of org.apache.logging.log4j.core.impl.Log4jContextFactory 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 3 with Log4jContextFactory

use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project hive by apache.

the class LogUtils method checkAndSetAsyncLogging.

public static boolean checkAndSetAsyncLogging(final Configuration conf) {
    final boolean asyncLogging = HiveConf.getBoolVar(conf, ConfVars.HIVE_ASYNC_LOG_ENABLED);
    if (asyncLogging) {
        System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        // default is ClassLoaderContextSelector which is created during automatic logging
        // initialization in a static initialization block.
        // Changing ContextSelector at runtime requires creating new context factory which will
        // internally create new context selector based on system property.
        LogManager.setFactory(new Log4jContextFactory());
    }
    return asyncLogging;
}
Also used : Log4jContextFactory(org.apache.logging.log4j.core.impl.Log4jContextFactory)

Example 4 with Log4jContextFactory

use of org.apache.logging.log4j.core.impl.Log4jContextFactory in project logging-log4j2 by apache.

the class ShutdownCallbackRegistryTest method testShutdownCallbackRegistry.

@Test
public void testShutdownCallbackRegistry() throws Exception {
    final LoggerContext context = ctx.getLoggerContext();
    assertTrue("LoggerContext should be started", context.isStarted());
    assertThat(Registry.CALLBACKS, hasSize(1));
    Registry.shutdown();
    assertTrue("LoggerContext should be stopped", context.isStopped());
    assertThat(Registry.CALLBACKS, hasSize(0));
    final ContextSelector selector = ((Log4jContextFactory) LogManager.getFactory()).getSelector();
    assertThat(selector.getLoggerContexts(), not(hasItem(context)));
}
Also used : Log4jContextFactory(org.apache.logging.log4j.core.impl.Log4jContextFactory) ContextSelector(org.apache.logging.log4j.core.selector.ContextSelector) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 5 with Log4jContextFactory

use of org.apache.logging.log4j.core.impl.Log4jContextFactory 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

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