Logger.isdebugenabled () Effect

Source: Internet
Author: User

It is often used in projects:
if (logger.isdebugenabled ()) {
Logger.debug ("message:" + user.getmessage ());
}
We would take it for granted that this code is intended to control the output of the log file, but by looking at the source we will find that the debug () method is the same as the isdebugenabled () method, Therefore, we can directly call the debug method where the output log is needed to achieve the purpose of controlling the output.
The following is the source code for the isdebugenabled () and Debug () methods:

public Boolean isdebugenabled () {

if (repository.isdisabled (Level.debug_int))

return false;

Return Level.DEBUG.isGreaterOrEqual (This.geteffectivelevel ());

}

public void Debug (Object message) {

if (repository.isdisabled (Level.debug_int))

Return

if (Level.DEBUG.isGreaterOrEqual (This.geteffectivelevel ())) {

Forcedlog (FQCN, level.debug, message, NULL);

}

}

So why do we have to superfluous and add such a judgment statement?

The official argument is that, for efficiency reasons, it depends on the situation.

First we look at the following code:

Log.debug ("message:" + user.getmessage ());
If our log level is info, then this statement will not be output, but this method will still be called, so the GetMessage () method will still execute. If the GetMessage () method is too complex, it takes a long time to execute. When the parameters are constructed, we enter the debug () method to determine:
if (repository.isdisabled (Level.debug_int))
Return
It's back here. This leads us to do nothing but takes time, and if the concurrency is large, the impact on performance is more pronounced.
At this time, for performance reasons, we should add isdebugenabled () to judge
if (logger.isdebugenabled ()) {

Logger.debug ("message:" + user.getmessage ());

}
But if it's such a simple output:
Logger.debug ("error");
Adding a judgment is not necessary.

Logger.isdebugenabled () Effect

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.