@Override public synchronized void start() { if (this.isStopped() || this.isStopping()) { throw new IllegalStateException("Cannot start this Log4jWebInitializerImpl after it was stopped."); } // only do this once if (this.isInitialized()) { super.setStarting(); this.name = this.substitutor.replace(this.servletContext.getInitParameter(LOG4J_CONTEXT_NAME)); final String location = this.substitutor.replace(this.servletContext .getInitParameter(LOG4J_CONFIG_LOCATION)); final boolean isJndi = "true".equalsIgnoreCase(this.servletContext .getInitParameter(IS_LOG4J_CONTEXT_SELECTOR_NAMED)); if (isJndi) { this.initializeJndi(location); } else { this.initializeNonJndi(location); } if (this.loggerContext instanceof AsyncLoggerContext) { ((AsyncLoggerContext) this.loggerContext).setUseThreadLocals(false); } this.servletContext.setAttribute(CONTEXT_ATTRIBUTE, this.loggerContext); super.setStarted(); } }
private static void workAroundLog4j2_5Bug() { // use reflection so we can use the same test with older versions of log4j2 try { final Method setUseThreadLocals = AsyncLoggerContext.class.getDeclaredMethod("setUseThreadLocals", new Class[]{boolean.class}); final LoggerContext context = LogManager.getContext(false); setUseThreadLocals.invoke(context, new Object[] {Boolean.TRUE}); } catch (final Throwable ignored) { } }
public static void reregisterMBeansAfterReconfigure(final MBeanServer mbs) { if (isJmxDisabled()) { LOGGER.debug("JMX disabled for Log4j2. Not registering MBeans."); return; } // now provide instrumentation for the newly configured // LoggerConfigs and Appenders try { final ContextSelector selector = getContextSelector(); if (selector == null) { LOGGER.debug("Could not register MBeans: no ContextSelector found."); return; } LOGGER.trace("Reregistering MBeans after reconfigure. Selector={}", selector); final List<LoggerContext> contexts = selector.getLoggerContexts(); int i = 0; for (final LoggerContext ctx : contexts) { LOGGER.trace("Reregistering context ({}/{}): '{}' {}", ++i, contexts.size(), ctx.getName(), ctx); // first unregister the context and all nested loggers, // appenders, statusLogger, contextSelector, ringbuffers... unregisterLoggerContext(ctx.getName(), mbs); final LoggerContextAdmin mbean = new LoggerContextAdmin(ctx, executor); register(mbs, mbean, mbean.getObjectName()); if (ctx instanceof AsyncLoggerContext) { final RingBufferAdmin rbmbean = ((AsyncLoggerContext) ctx).createRingBufferAdmin(); if (rbmbean.getBufferSize() > 0) { // don't register if Disruptor not started (DefaultConfiguration: config not found) register(mbs, rbmbean, rbmbean.getObjectName()); } } // register the status logger and the context selector // repeatedly // for each known context: if one context is unregistered, // these MBeans should still be available for the other // contexts. registerStatusLogger(ctx.getName(), mbs, executor); registerContextSelector(ctx.getName(), selector, mbs, executor); registerLoggerConfigs(ctx, mbs, executor); registerAppenders(ctx, mbs, executor); } } catch (final Exception ex) { LOGGER.error("Could not register mbeans", ex); } }
/** * Builds the configuration with {@link #build()} and then * reconfigures the Apache Log4j2 context. The process also * installs a shut-down hook to stop the context: * * <ul> * <li>Builds the configuration into an instance of * {@link EmbeddedConfiguration} using {@link #build()}. * <li>Sets the default {@link ConfigurationFactory} to * {@link EmbeddedConfigurationFactory} using * {@link ConfigurationFactory#setConfigurationFactory(ConfigurationFactory)} * . * <li>If asynchronous loggers are enabled, creates an * instance of {@link AsyncLogger}. If not, creates an * instance of {@link LoggerContext}. * <li>Reconfigures the context using * {@link LoggerContext#reconfigure()}. * <li>Starts the context using * {@link LoggerContext#start()}. * <li>Installs a shut down hook to * {@link Runtime#addShutdownHook(Thread)} to ensure the * logger context is stopped with * {@link LoggerContext#stop()}. * </ul> * * <p> * After running this method, the user is able to use Apache * Log4j as if it was configured using an instance of * {@code log4j.xml}. * * @see ConfigurationFactory * @see LoggerContext#reconfigure() * @see EmbeddedConfigurationFactory * @see EmbeddedConfiguration */ public final void configure() { final LoggerContext context = asyncLoggers ? (AsyncLoggerContext) LogManager.getContext(ConfigurationBuilder.class.getClassLoader(), false) : (LoggerContext) LogManager.getContext(ConfigurationBuilder.class.getClassLoader(), false); final AbstractConfiguration configuration = build(); final EmbeddedConfigurationFactory factory = new EmbeddedConfigurationFactory(configuration); ConfigurationFactory.setConfigurationFactory(factory); context.reconfigure(); context.start(); configuration.start(); final String rootLoggerName = LogManager.ROOT_LOGGER_NAME; final Logger logger = LogManager.getLogger(rootLoggerName); logger.info("Initialized logging configuration: {} (async-loggers={})", configuration.getName(), logger instanceof AsyncLogger); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { logger.info("Stopping logging configuration: {}", configuration.getName()); try { configuration.stop(); context.stop(); } catch (Exception e) { System.err.println(e); } } }, rootLoggerName)); }
@Setup public void setup() { loggerContext = new AsyncLoggerContext( "benchmark" ); }