If I have a java util logging statement such as
logger.log(LEVEL.FINE, "data buffer = {0}",
CommonUtils.prepareDataBufferString(dataBuffer));
Now even when my log level is not at FINE, the expensive prepare.. method still gets called,
which is not what I want to happen. I end up checking the logger level before this statement
if(logger.isLoggable(LEVEL.FINE)){
bufferString = CommonUtils.prepareDataBufferString(dataBuffer);
}
logger.log(LEVEL.FINE, "data buffer = {0}", bufferString);
this increases the lines of code unnecessarily. Can I avoid having to do this somehow. please help.
A technique like this might help.
FYI, it’s not the log framework calling the method, it’s Java. Java requires that you evaluate all the parameters to a method before the method can be invoked (this is called eager evaluation, contrast to lazy evaluation).