public void setup( final Log4j20Benchmark b ) { logger = new AsyncLogger( b.loggerContext, "", StringFormatterMessageFactory.INSTANCE ); }
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { AsyncLogger.stop(); }
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { applicationCtx.shutdown(); AsyncLogger.stop(); }
public static void resetClocks() throws IllegalAccessException { resetClock(Log4jLogEvent.class); resetClock(AsyncLogger.class); }
public static void main(final String[] args) throws Exception { System.setProperty("Log4jContextSelector", AsyncLoggerContextSelector.class.getName()); final Logger logger = LogManager.getLogger(); if (!(logger instanceof AsyncLogger)) { throw new IllegalStateException(); } // work around a bug in Log4j-2.5 workAroundLog4j2_5Bug(); logger.error("Starting..."); System.out.println("Starting..."); Thread.sleep(100); final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); final long testStartNanos = System.nanoTime(); final long[] UPTIMES = new long[1024]; final long[] DURATIONS = new long[1024]; // warmup final long startMs = System.currentTimeMillis(); final long end = startMs + TimeUnit.SECONDS.toMillis(10); int warmupCount = 0; do { runTest(logger, runtimeMXBean, UPTIMES, DURATIONS, warmupCount); warmupCount++; // Thread.sleep(1000);// drain buffer } while (System.currentTimeMillis() < end); final int COUNT = 10; for (int i = 0; i < COUNT; i++) { final int count = warmupCount + i; runTest(logger, runtimeMXBean, UPTIMES, DURATIONS, count); // Thread.sleep(1000);// drain buffer } final double testDurationNanos = System.nanoTime() - testStartNanos; System.out.println("Done. Calculating stats..."); printReport("Warmup", UPTIMES, DURATIONS, 0, warmupCount); printReport("Test", UPTIMES, DURATIONS, warmupCount, COUNT); final StringBuilder sb = new StringBuilder(512); sb.append("Test took: ").append(testDurationNanos/(1000.0*1000.0*1000.0)).append(" sec"); System.out.println(sb); final List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); for (int i = 0; i < gcBeans.size(); i++) { final GarbageCollectorMXBean gcBean = gcBeans.get(i); sb.setLength(0); sb.append("GC[").append(gcBean.getName()).append("] "); sb.append(gcBean.getCollectionCount()).append(" collections, collection time="); sb.append(gcBean.getCollectionTime()).append(" millis."); System.out.println(sb); } }
/** * 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)); }