Transferred from: http://huangxx.iteye.com/blog/190693
When using the log framework such as Log4j,common-log, it is found in a lot of code that writes
if (log.isdebugenabled ()) {
Log.debug ("xxxx");
}
I am very strange, why should I be with log.isdebugenabled ()? Since Log.debug () does not have output at the specified output level for debug, why should I add a isdebugenabled () before the decision?
if (log.isdebugenabled ()) { log.debug (buildfullstring ())} If this buildfullstring is not very efficient, So if you write Log.debug directly (buildfullstring ()), it will not print the statement, but the buildfullstring is executed, so it's a waste of effort. So with isdebugenabled, you can avoid executing buildfullstring. This method is generally used in the case that the buildfullstring function is not very efficient.
At Log.debug ("* * * *"), the execution results of the parameters are constructed first. If the argument is a function expression, the function is executed first, and then the debug level is determined. Select whether to output the results according to the debug level.
Public void Debug (Object message, Throwable t) { if(! this. repository.isdisabled (10000)) { if(Level.DEBUG.isGreaterOrEqual ( This . Geteffectivelevel ())) { this. Forcedlog (FQCN, level.debug, message, T); }}}
This is the debug source in log4j, assuming our code is:
Log.debug (Builddebuginfo ()); ... builddebuginfo () { domuchthings (); }
Log.debug (Builddebuginfo ()); At execution time, you need to perform the first
Use of log.isdebugenabled ()