/ Published in: Java
The Log4J RollingFileAppender is actually synchronous.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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.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); } } } static final Logger log = Logger.getLogger(L4JRFAisSynchronous.class); static final long N = 20000; 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); for (int i = 0; i < N; i++) { log.info(delay + " " + i); } 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(); } super(layout, filename, append); } super(layout, filename); } @Override protected void subAppend(LoggingEvent arg0) { try { // ignore } super.subAppend(arg0); } } }