Our team has the policy of doing logging like
if (LOGGER.isDebugEnabled()) { LOGGER.debug("model[" + model + "]"); }
instead of simply calling the logging method like this:
LOGGER.debug("model[" + model + "]");
This practice is capable of leading to some performance improvement, but on the other hand it makes the codebase more complex. Our application has no performance problems, probably never will, the argument for the introduction of the policy was simply that it's a good practice, so it shall be used every time we are doing logging.
Do you think it's a good policy?
The guard statement (checking isDebugEnabled() ) is there to prevent potentially expensive computation of the log message when it involves invocation of the toString() methods of various objects and concatenating the results.
You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG. The Apache log4j manual has a section on Configuration. Show activity on this post.
If you can afford to loose 400 ms per million request ,you don't need the check.
You should use SLF4J and make log4j your implementation. With SLF4J, you can eliminate isDebugEnabled()
altogether by using parameterized messages.
See the section about logging performance in the slf4j FAQ
The following two lines will yield the exact same output. However, the second form will outperform the first form by a factor of at least 30, in case of a disabled logging statement.
logger.debug("The new entry is " + entry + ".");
logger.debug("The new entry is {}.", entry);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With