Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log debug enabled check in java

What is the point of having the if statement here? The severity level can be changed in log4j.xml config file. If the severity level is debug, it'll log the debug message, else it will not.

What is the significance of the if statement below?

   if (log.isDebugEnabled())
    {
        log.debug("I am logging something");
    }
like image 269
user2434 Avatar asked May 03 '12 09:05

user2434


People also ask

How do I view log debugging?

To view a debug log, from Setup, enter Debug Logs in the Quick Find box, then select Debug Logs. Then click View next to the debug log that you want to examine. Click Download to download the log as an XML file. Debug logs have the following limits.

How do I enable debug logging?

Navigate to Event Viewer (Local)\Applications and Service Logs\Microsoft\User Experience Virtualization\App Agent. Right-click on Debug under App Agent and select Enable Log. Select OK when presented with the "Analytic and Debug logs may lose events when they are enabled.

What does log debug do in Java?

Log4j has the following levels of logging: Debug is used a lot for debugging the application at development time. Every log message will appear to log files once this level is set. It basically belongs to developers. The INFO logging level is used to record messages about routine application operation.


2 Answers

In your example there is no need for an 'if' statement

But if you take the below example then you can see that we do a concatenation and if the log level is info then unneccesarily this concatenation operation will be done. For better performance we check this condition.

if (log.isDebugEnabled())
{
    log.debug("I am logging " + 1234 + ".");
}

Extra info:

Use slf4j to avoid such if conditions. The above statement can be rewritten in slf4j as follows,

log.debug("I am logging {} .", 1234);
like image 178
Sridhar G Avatar answered Sep 21 '22 01:09

Sridhar G


It's a performance optimisation. It's done to avoid evaluating the arguments to log.debug. It's only worth doing if constructing the log message is particularly expensive (e.g. serialising an XML document).

This is covered in some detail in the Short introduction to log4j.

like image 40
harto Avatar answered Sep 24 '22 01:09

harto