Just have the below "if" block before using log.info or log.debug or log.error
if(log.isInfoEnabled()
{
log.info(yourFunctionName + " :--- " + getVeryLargeString() + someVar);
}
Why?
Suppose the current log4j level is set to Error then log.info messages will not be displayed, it will take only few nanoseconds to execute log method but it may take time to execute the function call, if any, in the message.
Drawback of not using if condition
1) Unnecessary call to the functions in the log.info message
2) Many string objects will be created in the string pool
Drawback of using if condition-
1) For each and every log method call u have to have this condition, code will not be readable and not be clear much.
2) Extra if condition in many places.
Jdk1.5 has solution for this problem, it removes log calls automatically removed if they're not being used.
It is possible to do this using bytecode manipulation - searching for and removing calls to the logger as the class is loaded. The new Java 1.5 instrumentation classes make this particularly easy, by allowing ClassTransformer classes to be registered on the command line that are allowed to manipulate class files as they are loaded.
To use this code, launch Java with the arguments:
java -javaagent:net.surguy.logfilter.LogPreloader=debug [your own class]
The argument is optional, and can be one of "debug", "info", "warn", "error" or "fatal", corresponding to the standard Log4J log levels.
How does it work
As Java loads each class, it passes it to each of the registered ClassFileTransformers, which get a chance to alter its bytecode. It uses the ASM bytecode manipulation library to do so.