Log4j-Why you shoshould use "isdebugenabled "?

Source: Internet
Author: User

When using a log framework such as log4j and common-log, it is found that many codes are written in this way.
If (log. isdebugenabled ()){
Log. debug ("XXXX ");
}

I'm wondering why it's different from log. isdebugenabled ()? Since log. debug () does not have output when the output level is not specified as debug, why should I add an isdebugenabled () in front of it?

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Write a test.

The project is as follows. If you want to understand what is going on, you will understand it when you run it.

 

Log4j. Properties

Log4j. rootlogger = info, stdout </P> <p> log4j. appender. stdout = org. apache. log4j. leleappender <br/> log4j. appender. stdout. layout = org. apache. log4j. patternlayout <br/> log4j. appender. stdout. layout. conversionpattern = % d % P [% C]-% m % n </P> <p>

 

Testlogger. Java

Package COM. democreen. log4jtest; </P> <p> Import Org. apache. commons. logging. log; <br/> Import Org. apache. commons. logging. logfactory; </P> <p> public class testlogger {</P> <p> // logging object <br/> protected log = logfactory. getlog (testlogger. class); </P> <p> private void withoutcheck () {<br/> long starttime = system. currenttimemillis (); <br/> for (INT I = 0; I <10000000; I ++) {<br/> log. debug ("this is a test"); <br/>}< br/> system. out. println (<br/> (system. currenttimemillis ()-starttime) <br/> + "milliseconds"); <br/>}< br/> private void withcheck () {<br/> long starttime = system. currenttimemillis (); </P> <p> for (INT I = 0; I <10000000; I ++) {<br/> If (log. isdebugenabled () <br/> log. debug ("this is a test"); <br/>}< br/> system. out. println (<br/> (system. currenttimemillis ()-starttime) <br/> + "milliseconds"); <br/>}< br/> private void newcheck () {<br/> long starttime = system. currenttimemillis (); <br/> for (INT I = 0; I <10000000; I ++) {<br/> If (log. isdebugenabled () <br/> log. debug ("this" + "is" + "A" + "test"); <br/>}< br/> system. out. println (<br/> (system. currenttimemillis ()-starttime) <br/> + "milliseconds"); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> testlogger = new testlogger (); <br/> testlogger. withoutcheck (); <br/> testlogger. withcheck (); <br/> testlogger. newcheck (); <br/>}</P> <p >}< br/>

 

Running result:

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View Source Code:

 

I downloaded the source code of log4jand apache-log4j-1.2.15.zip.

 

 

Log. Class

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For more information:

Log print level order:

 

Debug info warn error fatal

For log4j. rootlogger = debug, console, and file, both Debug and subsequent levels are printed. If it is info, debug will be released, which has a level.

 

 

Analysis: we can see that the log-level judgment is also performed inside the debug method.

Obviously, the message parameter must be constructed before using this debug method. This is also the purpose of using the isdebugenabled method for determination.

 

If this sentence is not added before, the result is the same, but the efficiency may be different:

When the output level is debug, that is, when the log information is output, the if clause is not added for judgment, and there is almost no difference in efficiency;

When the output level is higher than debug, that is, no log output is required:

① If the parameters in the debug method are relatively simple (for example, directly written strings) and the if clause is not added for judgment, there is almost no difference in efficiency;

② If the parameters in the debug method are complicated (for example, when other functions are used for calculation or String concatenation), add the if clause to the preceding clause for judgment, it will improve the efficiency (otherwise, a lot of things (such as String concatenation) will be done in the Big Bang, and later I found that no log information needs to be output ).

 

The problem is very simple, so you can understand it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.