%d %p %c{1.} [%t] %m%n
通过添加
-DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
或者设置System系统属性,以使用“disruptor”开启最高性能。
package test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Main {
private static final Logger LOGGER = LogManager.getLogger(Main.class);
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[2];
for (int i = 0; i < threads.length; i++) {
threads[i] =
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
testLog();
}
}
});
}
long s = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
long e = System.currentTimeMillis();
System.out.println(e - s);
}
public static void testLog() {
LOGGER.debug("debug!!!");
LOGGER.info("info!!!");
LOGGER.warn("warn!!!");
LOGGER.error("error!!!");
LOGGER.fatal("fatal!!!");
}
}
log4j-core-2.3.jar
2.声明和log4j 1.2保持一致:
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(Main.class.getName());