Possible Duplicate:
In log4j, does checking isDebugEnabled before logging improve performance?
I have seen people using log4j in the manner below:
if(logger.isDebugEnabled())
{
logger.debug(" message ");
}
However, I checked the documentation for the logger.debug API and found that it checks if debug is enabled before logging the message. In that case, what is the point of writing extra the if?
Wouldn’t it be exactly the same to just write
logger.debug(" message ");
?
If you’re really just writing
then there’s no benefit. But consider:
You don’t want that string concatenation (and more expensive conversions, potentially – think about things that you might want to log, but which are costly to convert to strings) to be executed if the result is just going to be thrown away.
If logging has a significant performance impact in the normal case, that’s a disincentive to add logging code which could really help you in situations where suddenly you want to turn diagnostics on.