Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a need to do a if(log.isDebugEnabled()) { ... } check? [duplicate]

is there a need to do an explicit if(log.isDebugEnabled()) { ... } check?

I mean i have seen some post mentioning that log.debug("something") does an implicit call to see if debug mode logging has been enabled, before it does the logging. Am i missing something or is there an intermediary step that is to be performed before this is used?

Thanks!

log.debug("ResultSet rs is retrieved from OracleTypes"); 

vs

if(log.isDebugEnabled()){      log.debug("ResultSet rs is retrieved from OracleTypes"); } 

Edit: Did a write up on this: http://java.sg/whether-to-do-a-isdebugenabled-checking-before-printing-out-your-log-statement/

like image 295
Oh Chin Boon Avatar asked Jun 28 '11 09:06

Oh Chin Boon


1 Answers

The statement:

if(log.isDebugEnabled()){ 

Is used just for performance reasons. It's use is optional since it is called by the log method internally.

But now you ask if this check is made internally, so why should I use it? It's very simple: if you log something as simple as this:

log.debug("ResultSet rs is retrieved from OracleTypes"); 

Then you don't need to do any check. If you compose a string to log using the append operator (+) like this:

log.debug("[" + System.getTimeInMillis() + "] ResultSet rs is retrieved from OracleTypes"); 

In this case you should check if the log is enabled or not, because if it isn't, even if the log is not made, the string composition is. And I must remind you that the use of operator "+" to concatenate strings is very inefficient.

like image 82
Filipe Palrinhas Avatar answered Sep 29 '22 09:09

Filipe Palrinhas