Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enclosing calls to debug() in if isDebugEnabled(): a good policy?

Tags:

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?

like image 470
Chei Avatar asked Mar 17 '09 09:03

Chei


People also ask

What is isDebugEnabled?

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.

How do I enable debug mode in log4j properties?

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.

Do we need to check isDebugEnabled?

If you can afford to loose 400 ms per million request ,you don't need the check.


1 Answers

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);

like image 89
Aries McRae Avatar answered Sep 27 '22 22:09

Aries McRae