Revision: 39793
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 21, 2011 02:04 by cruelfate
Initial Code
package org.example; import java.io.IOException; import java.text.MessageFormat; import org.apache.log4j.Layout; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.RollingFileAppender; import org.apache.log4j.spi.LoggingEvent; public class L4JRFAisSynchronous { static RFA appender; static { System.setProperty("log4j.defaultInitOverride", "true"); // System.setProperty("log4j.debug", "true"); Logger rootLogger = Logger.getRootLogger(); if (!rootLogger.getAllAppenders().hasMoreElements()) { rootLogger.setLevel(Level.INFO); try { appender = new RFA(new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n"), "log/L4J.log", true); appender.setMaxFileSize("10240KB"); appender.setMaxBackupIndex(10); rootLogger.addAppender(appender); } catch (IOException e) { throw new RuntimeException("Appender configuration failed", e); } } } static final Logger log = Logger.getLogger(L4JRFAisSynchronous.class); static final long N = 20000; public static void main(String[] args) { System.out.println("Appender: " + appender.getClass().getSuperclass().toString()); System.out.println("# of iterations per trial = " + N); trial(0); trial(0); trial(0); trial(0); trial(0); trial(0); trial(1); trial(10); trial(100); trial(0); trial(1); trial(10); trial(0); trial(100); } private static void trial(int delay) { appender.setDelay(delay); long start = System.nanoTime(); for (int i = 0; i < N; i++) { log.info(delay + " " + i); } long end = System.nanoTime() - start; long total = end/N; System.out.println(MessageFormat.format("{0} nS. per append with delay = {1} nS. : appender time = {2} nS. per call.", total, delay, total - delay)); } public static class RFA extends RollingFileAppender { int delay = 0; public int getDelay() { return delay; } public void setDelay(int time) { this.delay = time; } public RFA() { super(); } public RFA(Layout layout, String filename, boolean append) throws IOException { super(layout, filename, append); } public RFA(Layout layout, String filename) throws IOException { super(layout, filename); } @Override protected void subAppend(LoggingEvent arg0) { try { Thread.sleep(0, delay); } catch (InterruptedException e) { // ignore } super.subAppend(arg0); } } }
Initial URL
Initial Description
The Log4J RollingFileAppender is actually synchronous.
Initial Title
A demonstration showing that Log4J RollingFileAppender is actually synchronous.
Initial Tags
Initial Language
Java