Note that there are some explanatory texts on larger screens.

plurals
  1. POIn log4j, does checking isDebugEnabled before logging improve performance?
    text
    copied!<p>I am using <strong><em>Log4J</em></strong> in my application for logging. Previously I was using debug call like:</p> <p><strong>Option 1:</strong></p> <pre><code>logger.debug("some debug text"); </code></pre> <p>but some links suggest that it is better to check <code>isDebugEnabled()</code> first, like: </p> <p><strong>Option 2:</strong></p> <pre><code>boolean debugEnabled = logger.isDebugEnabled(); if (debugEnabled) { logger.debug("some debug text"); } </code></pre> <p>So my question is "<strong>Does option 2 improve performance any way?</strong>".</p> <p>Because in any case Log4J framework have same check for debugEnabled. For option 2 it might be beneficial if we are using multiple debug statement in single method or class, where the framework does not need to call <code>isDebugEnabled()</code> method multiple times (on each call); in this case it calls <code>isDebugEnabled()</code> method only once, and if Log4J is configured to debug level then actually it calls <code>isDebugEnabled()</code> method twice:</p> <ol> <li>In case of assigning value to debugEnabled variable, and</li> <li>Actually called by logger.debug() method.</li> </ol> <p>I don't think that if we write multiple <code>logger.debug()</code> statement in method or class and calling <code>debug()</code> method according to option 1 then it is overhead for Log4J framework in comparison with option 2. Since <code>isDebugEnabled()</code> is a very small method (in terms of code), it might be good candidate for inlining.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload