Java Code Examples for org.apache.logging.log4j.LogManager#getFactory()

The following examples show how to use org.apache.logging.log4j.LogManager#getFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4jWebInitializerImpl.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 2
Source File: Server.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code ContextSelector} of the current {@code Log4jContextFactory}.
 *
 * @return the {@code ContextSelector} of the current {@code Log4jContextFactory}
 */
private static ContextSelector getContextSelector() {
    final LoggerContextFactory factory = LogManager.getFactory();
    if (factory instanceof Log4jContextFactory) {
        final ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
        return selector;
    }
    return null;
}
 
Example 3
Source File: Configurator.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static Log4jContextFactory getFactory() {
    final LoggerContextFactory factory = LogManager.getFactory();
    if (factory instanceof Log4jContextFactory) {
        return (Log4jContextFactory) factory;
    } else if (factory != null) {
        LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.",
                factory.getClass().getName(), Log4jContextFactory.class.getName());
        return null;
    } else {
        LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
        return null;
    }
}
 
Example 4
Source File: SpringEnvironmentHolder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
protected Environment getEnvironment() {
    if (environment == null && LogManager.getFactory() != null && LogManager.getFactory().hasContext(SpringEnvironmentHolder.class.getName(), null, false)) {
        lock.lock();
        try {
            if (environment == null) {
                Object obj = LogManager.getContext(false).getObject(Log4j2CloudConfigLoggingSystem.ENVIRONMENT_KEY);
                environment = obj instanceof Environment ? (Environment) obj : null;
            }
        } finally {
            lock.unlock();
        }
    }
    return environment;
}
 
Example 5
Source File: LogManagerLoggerContextFactoryRule.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected void before() throws Throwable {
    this.restoreLoggerContextFactory = LogManager.getFactory();
    LogManager.setFactory(this.loggerContextFactory);
}
 
Example 6
Source File: KubernetesLookup.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private boolean isSpringActive() {
    return isSpringIncluded && LogManager.getFactory() != null
        && LogManager.getFactory().hasContext(KubernetesLookup.class.getName(), null, false)
        && LogManager.getContext(false).getObject(SPRING_ENVIRONMENT_KEY) != null;
}