Home » » How to efficiently use LOG4J?

How to efficiently use LOG4J?

How to efficiently use LOG4J?

As we may all know LOG4J is very powerful tool for building the log system. However, do you know how to use LOG4J efficiently?
Since if we misuse this monster, it will consume a lot of your resources.

1. never log information before judging the log level

for example
static Logger logger = Logger.getLogger(AbstractExternalizable.class);
logger.debug(“debug: ”  + getMessage());
static void getMessage(){
for(int i=0; i< 1000; i++){
System.out.println(“” + i);
}
}
For this snippet of codes, even if you set the log level to “info”,  the method “getMessage()” will still be executed.
This is not what we anticipated, and it will cost a lot. So if we judge the log level first, it will be all different.
That is, if( logger.isDebugEnabled() ) { logger.debug(“debug: ”  + getMessage());  } .
In this case, the method “getMessage()” will not be called at all.
Recently, I encountered this problem in LabLucene. When I revised this codes as mentioned above, the program runs magically fast.
Basic run on WT10G with 100 queries: 13s –> 7.68, QE run: 103 –> 52

2. set buffer for log

1 Comments:

write my dissertation for me said...

Using LOG4J efficiently can significantly improve application performance by avoiding unnecessary method calls and resource consumption. Checking log levels before generating debug messages is a simple but effective optimisation technique. These programming practices are useful for students working on software development projects and technical research. Those managing demanding academic workloads may also explore write my dissertation for me for writing support.

Popular Posts