Monday, September 6, 2010

Recommand to check LOGGER.isDebugEnabled when writing log statements....

If you observe most of the members will test the logger level and then they will wrote the logger statements.

Example:- LOGGER.isDebugEnabled or LOGGER.isInfoEnabled

Logger hierarchy ( DEBUG INFO WARN ERROR FATAL )

Let say we have few logger statements in our project.

Example:

logger.debug("Sample debug message");

logger.info("Sample info message");

logger.warn("Sample warn message");

logger.error("Sample error message");

logger.fatal("Sample fatal message");


Assume that your logger level is set to INFO then we should only see the message those are

Sample info message

Sample warn message

Sample error message

Sample fatal message

It works fine but it will evaluate logger.debug("Sample debug message"); statement even though it won't print because our logger level is info.

If you take memory and time factor into the consideration then use like this....

if(LOGGER.isDebugEnabled){
logger.debug("Sample debug message");
}
...........then it won't evaluate logger.debug("Sample debug message"); statement. It will check the condition and terminates because as per our discussion currently INFO is Enabled.

No comments:

Post a Comment