org.apache.logging.log4j.spi.LoggerContextFactory Java Examples

The following examples show how to use org.apache.logging.log4j.spi.LoggerContextFactory. 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: Activator.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public void start(final BundleContext bundleContext) throws Exception {
    ProviderUtil.STARTUP_LOCK.lock();
    lockingProviderUtil = true;
    final BundleWiring self = bundleContext.getBundle().adapt(BundleWiring.class);
    final List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
    for (final BundleWire wire : required) {
        loadProvider(bundleContext, wire.getProviderWiring());
    }
    bundleContext.addBundleListener(this);
    final Bundle[] bundles = bundleContext.getBundles();
    for (final Bundle bundle : bundles) {
        loadProvider(bundle);
    }
    unlockIfReady();
}
 
Example #2
Source File: AsyncAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private LoggerContext configure(String configLocation) throws Exception {
    File file = new File(configLocation);
    InputStream is = new FileInputStream(file);
    ConfigurationSource source = new ConfigurationSource(is, file);
    LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
    LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration;
    if (configLocation.endsWith(".xml")) {
        configuration = new XmlConfigurationFactory().getConfiguration(context, source);
    } else {
        configuration = new PropertiesConfigurationFactory().getConfiguration(context, source);
    }
    assertNotNull("No configuration created", configuration);
    Configurator.reconfigure(configuration);
    return context;
}
 
Example #3
Source File: Logging.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<? extends LoggerContext> getContexts() {
    LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
    if(factory instanceof SimpleLoggerContextFactory) {
        return Collections.singletonList(factory.getContext(null, null, null, true));
    }
    return ((Log4jContextFactory) org.apache.logging.log4j.LogManager.getFactory()).getSelector().getLoggerContexts();
}
 
Example #4
Source File: XmlConfigurationTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private LoggerContext configure(String configLocation) throws Exception {
    File file = new File(configLocation);
    InputStream is = new FileInputStream(file);
    ConfigurationSource source = new ConfigurationSource(is, file);
    LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
    LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = new XmlConfigurationFactory().getConfiguration(context, source);
    assertNotNull("No configuration created", configuration);
    Configurator.reconfigure(configuration);
    return context;
}
 
Example #5
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 #6
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 #7
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 #8
Source File: LogManagerLoggerContextFactoryRule.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public LogManagerLoggerContextFactoryRule(final LoggerContextFactory loggerContextFactory) {
    super();
    this.loggerContextFactory = loggerContextFactory;
}
 
Example #9
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the current LoggerContextFactory.
 *
 * @return The LoggerContextFactory.
 */
public static LoggerContextFactory getFactory() {
    return factory;
}
 
Example #10
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the current LoggerContextFactory to use. Normally, the appropriate LoggerContextFactory is created at
 * startup, but in certain environments, a LoggerContextFactory implementation may not be available at this point.
 * Thus, an alternative LoggerContextFactory can be set at runtime.
 *
 * <p>
 * Note that any Logger or LoggerContext objects already created will still be valid, but they will no longer be
 * accessible through LogManager. Thus, <strong>it is a bad idea to use this method without a good reason</strong>!
 * Generally, this method should be used only during startup before any code starts caching Logger objects.
 * </p>
 *
 * @param factory the LoggerContextFactory to use.
 */
// FIXME: should we allow only one update of the factory?
public static void setFactory(final LoggerContextFactory factory) {
    LogManager.factory = factory;
}