Java – performance of log4j
I am developing a web application. I want to record some information to help me improve and observe the application (I'm using Tomcat6)
First, I thought I would use stringbuilders to attach logs to them, and a task would continue to the database, just like every 2 minutes Because I'm worried about the performance of the out of the box recording system Then I did some tests Especially log4j
This is my code:
Main. java
public static void main(String[] args) { Thread[] threads = new Thread[LoggerThread.threadsNumber]; for(int i = 0; i < LoggerThread.threadsNumber; ++i){ threads[i] = new Thread(new LoggerThread("name - " + i)); } LoggerThread.startTimestamp = System.currentTimeMillis(); for(int i = 0; i < LoggerThread.threadsNumber; ++i){ threads[i].start(); }
LoggerThread. java
public class LoggerThread implements Runnable{ public static int threadsNumber = 10; public static long startTimestamp; private static int counter = 0; private String name; public LoggerThread(String name) { this.name = name; } private Logger log = Logger.getLogger(this.getClass()); @Override public void run() { for(int i=0; i<10000; ++i){ log.info(name + ": " + i); if(i == 9999){ int c = increaseCounter(); if(c == threadsNumber){ System.out.println("Elapsed time: " + (System.currentTimeMillis() - startTimestamp)); } } } } private synchronized int increaseCounter(){ return ++counter; } } }
log4j. properties
log4j.logger.main.LoggerThread=debug,f log4j.appender.f=org.apache.log4j.RollingFileAppender log4j.appender.f.layout=org.apache.log4j.PatternLayout log4j.appender.f.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender.f.File=c:/logs/logging.log log4j.appender.f.MaxFileSize=15000KB log4j.appender.f.MaxBackupIndex=50
I think this is a very common configuration of log4j First, I use log4j 1.2 Then I realized there was a newer version, so I switched to 1.2 sixteen
Here are numbers (all in milliseconds)
LoggerThread. threadsNumber = 10
1.2.14: 4235,4267,4328,4282 1.2.16: 2780,2781,2797,2781
LoggerThread. threadsNumber = 100
1.2.14: 41312,41014,42251 1.2.16: 25606,25729,25922
I think it's fast Don't forget: in each loop, the running method is not just logging into the file, it must connect the string (name ":" I) and check one if test (I = = 9999)
When threadsnumber is 10, there are 100.000 logs, if testing and connecting When it is 100, there is 1.000 000 log if testing and connecting I've read somewhere that the JVM uses additional connections from StringBuilder instead of simple connections
What did I miss? Did I do wrong? Did I forget anything that might degrade performance? If these numbers are correct, I don't think I have to worry about the performance of log4j, even if I log in a lot, right?
I read: "the typical cost of actual recording is about 100 to 300 microseconds." Is that right? (log4J manual)
Solution
Yes, log4j is considered fast because of the conscious efforts of its implementers See also the section "performance" at the end of this 07 / 08