/** * We cannot presume that the default file name/location setting won't be changed by the user. * Therefore, we should be able to retrieve that info from the underlying logging mechanism * by iterating over appenders. */ private List<Path> getActiveLogFilePaths() { LoggerContextFactory factory = LogManager.getFactory(); ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); List<Path> fileNameList = new ArrayList<>(); for (LoggerContext ctx : selector.getLoggerContexts()) { for (Appender appender : ctx.getConfiguration().getAppenders().values()) { String fileName = extractFileName(appender); if (fileName != null) { fileNameList.add(Paths.get(fileName)); } } } return fileNameList; }
@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(); }
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(); }
private void initializeJndi(final String location) throws UnavailableException { URI configLocation = null; if (location != null) { try { configLocation = new URI(location); } catch (final Exception e) { this.servletContext.log("Unable to convert configuration location [" + location + "] to a URI!", e); } } if (this.name == null) { throw new UnavailableException("A log4jContextName context parameter is required"); } LoggerContext loggerContext; final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { final ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); if (selector instanceof NamedContextSelector) { this.selector = (NamedContextSelector) selector; loggerContext = this.selector.locateContext(this.name, this.servletContext, configLocation); ContextAnchor.THREAD_CONTEXT.set(loggerContext); if (loggerContext.getStatus() == LoggerContext.Status.INITIALIZED) { loggerContext.start(); } ContextAnchor.THREAD_CONTEXT.remove(); } else { this.servletContext.log("Potential problem: Selector is not an instance of NamedContextSelector."); return; } } else { this.servletContext.log("Potential problem: Factory is not an instance of Log4jContextFactory."); return; } this.loggerContext = loggerContext; this.servletContext.log("Created logger context for [" + this.name + "] using [" + loggerContext.getClass().getClassLoader() + "]."); }
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()); }
/** * 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; }
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; } }
public LogManagerLoggerContextFactoryRule(final LoggerContextFactory loggerContextFactory) { super(); this.loggerContextFactory = loggerContextFactory; }
/** * Returns the LoggerContextFactory. * @return The LoggerContextFactory. */ public static LoggerContextFactory getFactory() { return factory; }
/** * Returns the current LoggerContextFactory. * * @return The LoggerContextFactory. */ public static LoggerContextFactory getFactory() { return factory; }
/** * 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; }