Search in sources :

Example 1 with NamedContextSelector

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

URI (java.net.URI)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 AsyncLoggerContext (org.apache.logging.log4j.core.async.AsyncLoggerContext)1 Log4jContextFactory (org.apache.logging.log4j.core.impl.Log4jContextFactory)1 ContextSelector (org.apache.logging.log4j.core.selector.ContextSelector)1 NamedContextSelector (org.apache.logging.log4j.core.selector.NamedContextSelector)1 LoggerContextFactory (org.apache.logging.log4j.spi.LoggerContextFactory)1