Java Code Examples for org.apache.logging.log4j.core.LoggerContext#getExternalContext()

The following examples show how to use org.apache.logging.log4j.core.LoggerContext#getExternalContext() . 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: Log4jContextFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the LoggerContext using the ContextSelector.
 * @param fqcn The fully qualified class name of the caller.
 * @param loader The ClassLoader to use or null.
 * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
 * @param currentContext If true returns the current Context, if false returns the Context appropriate
 * for the caller if a more appropriate Context can be determined.
 * @param source The configuration source.
 * @return The LoggerContext.
 */
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext, final ConfigurationSource source) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        if (source != null) {
            ContextAnchor.THREAD_CONTEXT.set(ctx);
            final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, source);
            LOGGER.debug("Starting LoggerContext[name={}] from configuration {}", ctx.getName(), source);
            ctx.start(config);
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            ctx.start();
        }
    }
    return ctx;
}
 
Example 2
Source File: Log4jContextFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the LoggerContext using the ContextSelector using the provided Configuration
 * @param fqcn The fully qualified class name of the caller.
 * @param loader The ClassLoader to use or null.
 * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
 * @param currentContext If true returns the current Context, if false returns the Context appropriate
 * for the caller if a more appropriate Context can be determined.
 * @param configuration The Configuration.
 * @return The LoggerContext.
 */
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
        final boolean currentContext, final Configuration configuration) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, null);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        ContextAnchor.THREAD_CONTEXT.set(ctx);
        try {
            ctx.start(configuration);
        } finally {
            ContextAnchor.THREAD_CONTEXT.remove();
        }
    }
    return ctx;
}
 
Example 3
Source File: Log4jContextFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the LoggerContext using the ContextSelector.
 * @param fqcn The fully qualified class name of the caller.
 * @param loader The ClassLoader to use or null.
 * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
 * @param currentContext If true returns the current Context, if false returns the Context appropriate
 * for the caller if a more appropriate Context can be determined.
 * @param configLocation The location of the configuration for the LoggerContext (or null).
 * @return The LoggerContext.
 */
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext, final URI configLocation, final String name) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext, configLocation);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (name != null) {
    	ctx.setName(name);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        if (configLocation != null || name != null) {
            ContextAnchor.THREAD_CONTEXT.set(ctx);
            final Configuration config = ConfigurationFactory.getInstance().getConfiguration(ctx, name, configLocation);
            LOGGER.debug("Starting LoggerContext[name={}] from configuration at {}", ctx.getName(), configLocation);
            ctx.start(config);
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            ctx.start();
        }
    }
    return ctx;
}
 
Example 4
Source File: SOFAConfigurationFactory.java    From sofa-common-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
    if (source != null && source != ConfigurationSource.NULL_SOURCE) {
        return loggerContext.getExternalContext() != null ? new SOFAConfiguration()
            : new XmlConfiguration(loggerContext, source);
    }
    return null;
}
 
Example 5
Source File: Log4jContextFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the LoggerContext using the ContextSelector.
 * @param fqcn The fully qualified class name of the caller.
 * @param loader The ClassLoader to use or null.
 * @param currentContext If true returns the current Context, if false returns the Context appropriate
 * for the caller if a more appropriate Context can be determined.
 * @param externalContext An external context (such as a ServletContext) to be associated with the LoggerContext.
 * @return The LoggerContext.
 */
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext) {
    final LoggerContext ctx = selector.getContext(fqcn, loader, currentContext);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        ctx.start();
    }
    return ctx;
}
 
Example 6
Source File: Log4jContextFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
        final boolean currentContext, final List<URI> configLocations, final String name) {
    final LoggerContext ctx = selector
            .getContext(fqcn, loader, currentContext, null/*this probably needs to change*/);
    if (externalContext != null && ctx.getExternalContext() == null) {
        ctx.setExternalContext(externalContext);
    }
    if (name != null) {
        ctx.setName(name);
    }
    if (ctx.getState() == LifeCycle.State.INITIALIZED) {
        if ((configLocations != null && !configLocations.isEmpty())) {
            ContextAnchor.THREAD_CONTEXT.set(ctx);
            final List<AbstractConfiguration> configurations = new ArrayList<>(configLocations.size());
            for (final URI configLocation : configLocations) {
                final Configuration currentReadConfiguration = ConfigurationFactory.getInstance()
                        .getConfiguration(ctx, name, configLocation);
                if (currentReadConfiguration instanceof AbstractConfiguration) {
                    configurations.add((AbstractConfiguration) currentReadConfiguration);
                } else {
                    LOGGER.error(
                            "Found configuration {}, which is not an AbstractConfiguration and can't be handled by CompositeConfiguration",
                            configLocation);
                }
            }
            final CompositeConfiguration compositeConfiguration = new CompositeConfiguration(configurations);
            LOGGER.debug("Starting LoggerContext[name={}] from configurations at {}", ctx.getName(),
                    configLocations);
            ctx.start(compositeConfiguration);
            ContextAnchor.THREAD_CONTEXT.remove();
        } else {
            ctx.start();
        }
    }
    return ctx;
}