Search in sources :

Example 1 with ContextSelector

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

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

use of org.apache.logging.log4j.core.selector.ContextSelector in project logging-log4j2 by apache.

the class Server method reregisterMBeansAfterReconfigure.

public static void reregisterMBeansAfterReconfigure(final MBeanServer mbs) {
    if (isJmxDisabled()) {
        LOGGER.debug("JMX disabled for Log4j2. Not registering MBeans.");
        return;
    }
    // LoggerConfigs and Appenders
    try {
        final ContextSelector selector = getContextSelector();
        if (selector == null) {
            LOGGER.debug("Could not register MBeans: no ContextSelector found.");
            return;
        }
        LOGGER.trace("Reregistering MBeans after reconfigure. Selector={}", selector);
        final List<LoggerContext> contexts = selector.getLoggerContexts();
        int i = 0;
        for (final LoggerContext ctx : contexts) {
            LOGGER.trace("Reregistering context ({}/{}): '{}' {}", ++i, contexts.size(), ctx.getName(), ctx);
            // first unregister the context and all nested loggers,
            // appenders, statusLogger, contextSelector, ringbuffers...
            unregisterLoggerContext(ctx.getName(), mbs);
            final LoggerContextAdmin mbean = new LoggerContextAdmin(ctx, executor);
            register(mbs, mbean, mbean.getObjectName());
            if (ctx instanceof AsyncLoggerContext) {
                final RingBufferAdmin rbmbean = ((AsyncLoggerContext) ctx).createRingBufferAdmin();
                if (rbmbean.getBufferSize() > 0) {
                    // don't register if Disruptor not started (DefaultConfiguration: config not found)
                    register(mbs, rbmbean, rbmbean.getObjectName());
                }
            }
            // register the status logger and the context selector
            // repeatedly
            // for each known context: if one context is unregistered,
            // these MBeans should still be available for the other
            // contexts.
            registerStatusLogger(ctx.getName(), mbs, executor);
            registerContextSelector(ctx.getName(), selector, mbs, executor);
            registerLoggerConfigs(ctx, mbs, executor);
            registerAppenders(ctx, mbs, executor);
        }
    } catch (final Exception ex) {
        LOGGER.error("Could not register mbeans", ex);
    }
}
Also used : ContextSelector(org.apache.logging.log4j.core.selector.ContextSelector) AsyncLoggerContext(org.apache.logging.log4j.core.async.AsyncLoggerContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) AsyncLoggerContext(org.apache.logging.log4j.core.async.AsyncLoggerContext) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException)

Example 4 with ContextSelector

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

ContextSelector (org.apache.logging.log4j.core.selector.ContextSelector)4 LoggerContext (org.apache.logging.log4j.core.LoggerContext)3 Log4jContextFactory (org.apache.logging.log4j.core.impl.Log4jContextFactory)3 AsyncLoggerContext (org.apache.logging.log4j.core.async.AsyncLoggerContext)2 Test (org.junit.Test)2 URI (java.net.URI)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)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 LoggerContextFactory (org.apache.logging.log4j.spi.LoggerContextFactory)1