It really depends on how you wrote those logging instructions. If you wrote:
logger.debug(computeSomeCostlyDebugOutput());
You might affect performance badly if the logger is not set on a DEBUG level (computeSomeCostlyDebugOutput
will always take time to execute and its result will then be ignored by the logger if not matching the DEBUG level).
If you write it like this instead:
if (logger.isDebugEnabled()) {
logger.debug(computeSomeCostlyDebugOutput());
}
then the costly operations and logging will occur only if the correct logger level is set (i.e. the logger won't ignore it). It basically acts like another switch for the logger, the first switch being the configured logger level.
As Andrzej Doyle very well pointed out, the logger will check its level internally, but this happens in the debug
method, after time was already wasted in the computeSomeCostlyDebugOutput
. If you waste time in computeSomeCostlyDebugOutput
, you better do it when you know that its result won't be in vain.
A lot of software ships with logging instructions which you can activate if you need more details into the inner workings of the thing, execution path and stuff. Just make sure they can be deactivated and only take computing time if the appropriate level is set.