@Test public void regionClassLoader() { RegionClassLoader regionClassLoader = mock(RegionClassLoader.class); withContextClassLoader(regionClassLoader, () -> { logger.info(MESSAGE); verify(contextSelector).getContext(LOGGER_NAME, regionClassLoader, true); }); } }
@Test public void testShutdownCallbackRegistry() throws Exception { final LoggerContext context = ctx.getLoggerContext(); assertTrue("LoggerContext should be started", context.isStarted()); assertThat(Registry.CALLBACKS, hasSize(1)); Registry.shutdown(); assertTrue("LoggerContext should be stopped", context.isStopped()); assertThat(Registry.CALLBACKS, hasSize(0)); final ContextSelector selector = ((Log4jContextFactory) LogManager.getFactory()).getSelector(); assertThat(selector.getLoggerContexts(), not(hasItem(context))); }
/** * Removes knowledge of a LoggerContext. * * @param context The context to remove. */ @Override public void removeContext(final org.apache.logging.log4j.spi.LoggerContext context) { if (context instanceof LoggerContext) { selector.removeContext((LoggerContext) context); } }
private Logger getLogger(final ClassLoader resolvedCtxClassLoader) { if (useThisLoggerContextClassLoader(resolvedCtxClassLoader)) { return originalLogger; } Logger logger; // trick - this is probably a logger declared in a static field // the classloader used to create it and the TCCL can be different // ask contextSelector for the correct context if (contextSelector instanceof ArtifactAwareContextSelector) { logger = ((ArtifactAwareContextSelector) contextSelector).getContextWithResolvedContextClassLoader(resolvedCtxClassLoader) .getLogger(getName(), getMessageFactory()); } else { logger = contextSelector.getContext(getName(), resolvedCtxClassLoader, true).getLogger(getName(), getMessageFactory()); } if (logger instanceof DispatchingLogger) { return ((DispatchingLogger) logger).getLogger(resolvedCtxClassLoader); } else { return logger; } }
LoggerContextFactory factory = LogManager.getFactory(); ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); for (LoggerContext ctx : selector.getLoggerContexts()) { ... Map<String, Appender> m = ctx.getConfiguration().getAppenders(); ... }
private Logger getLogger() { final ClassLoader currentClassLoader = resolveLoggerContextClassLoader(Thread.currentThread().getContextClassLoader()); if (useThisLoggerContextClassLoader(currentClassLoader)) { return originalLogger; } // trick - this is probably a logger declared in a static field // the classloader used to create it and the TCCL can be different // ask contextSelector for the correct context return contextSelector.getContext(getName(), currentClassLoader, true).getLogger(getName(), getMessageFactory()); }
final List<LoggerContext> contexts = selector.getLoggerContexts(); int i = 0; for (final LoggerContext ctx : contexts) {
private Logger getLogger() { final ClassLoader currentClassLoader = resolveLoggerContextClassLoader(Thread.currentThread().getContextClassLoader()); if (useThisLoggerContextClassLoader(currentClassLoader)) { return originalLogger; } // trick - this is probably a logger declared in a static field // the classloader used to create it and the TCCL can be different // ask contextSelector for the correct context return contextSelector.getContext(getName(), currentClassLoader, true).getLogger(getName(), getMessageFactory()); }
/** * 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; }
/** * 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; }
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);
/** * 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; }
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);