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");
}
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With