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


sri said...

Thanks for sharing this informative content.,
Turient is an All-in-one platform for all our teaching needs. If Teaching is your passion ,enabling is ours
Read the Informative blog - 11 Free Teaching Tools for Online Teachers
Free Teaching Tools for Online Teachers

Popular Posts