本文整理匯總了Java中org.apache.logging.log4j.core.LoggerContext.setExternalContext方法的典型用法代碼示例。如果您正苦於以下問題:Java LoggerContext.setExternalContext方法的具體用法?Java LoggerContext.setExternalContext怎麽用?Java LoggerContext.setExternalContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.logging.log4j.core.LoggerContext
的用法示例。
在下文中一共展示了LoggerContext.setExternalContext方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initialize
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
/**
* Initializes the Logging Context.
* @param name The Context name.
* @param loader The ClassLoader for the Context (or null).
* @param configLocation The configuration for the logging context.
* @param externalContext The external context to be attached to the LoggerContext
* @return The LoggerContext.
*/
public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
final Object externalContext) {
try {
final org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader, false, configLocation);
if (context instanceof LoggerContext) {
final LoggerContext ctx = (LoggerContext) context;
ContextAnchor.THREAD_CONTEXT.set(ctx);
if (externalContext != null) {
ctx.setExternalContext(externalContext);
}
final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
ctx.start(config);
ContextAnchor.THREAD_CONTEXT.remove();
return ctx;
} else {
LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j",
context.getClass().getName(), LoggerContext.class.getName());
}
} catch (final Exception ex) {
ex.printStackTrace();
}
return null;
}
示例2: getContext
import org.apache.logging.log4j.core.LoggerContext; //導入方法依賴的package包/類
/**
* 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;
}