Hi,
My application uses log4j for logging and usually I do logging by checking whether a particular level is enabled and then log like the following
if(Logger.isApplicationDebugEnabled()){
Logger.logApplicationDebug("something"+values);
}
Now this if check reduces our branch coverage during jUnit testing. In order to overcome this, my friend suggested getting rid of "if" checks for logging.
- First one of my friends suggested removing the if check and log directly. The problem with this approach is that it might slow down the application due to formation of strings which would not end up in log files anyway.
The second approach I thought of was very similar to SLF4j.
Logger.debug("Number {0}, with date {1,date}",1234, new Date());
This felt very attractive and simple.(This solution internally uses MessageFormat.format(str, object[]) method. But I am concerned about the performance this might have. Also note that 'Logger' is my internal utility class and in each of its log methods, there is a check of Log enabling. What do you guys suggest? I went through velocity templates but it requires map of parameters. Is there any lightweight solution which just substitutes values based on param index location or is this solution okay?