Link to home
Start Free TrialLog in
Avatar of thesilentkiller
thesilentkiller

asked on

Industry Practice for logging

I am using the jdk logger which allows me to log messages at different log levels such as Severe, Warn, Info, Fine, Finer, Finest.

A typical log message looks like this:

logger.info("This is my log message");

This is fine. But if I have a log message such as:

logger.info(myObj.toString());

where myObj has a processor intensive toString method, then irrespective of the log level set, myObj.toString() would be executed. Even with a log level of Severe, myObj.toString() would be executed since it is done BEFORE a decision is made by the jdk logger not to log the message.

So, an alternate practice would be:

if(logger.getLevel() == Logger.INFO) {
    logger.info(myObj.toString());
}

However, for the first example, it will be:

if(logger.getLevel() == Logger.INFO) {
    logger.info("This is my log message");
}

which seems to be an overkill to have a simple log message.

Can anyone who has been using jdk logger suggest what the industry practice is? Should I use the if clause at all places? Should I use it only for log messages which have a processor intensive log message? Any other way of handling this issue?

Thanks,
SK
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Industry practice is to use log4j
... and there the sub-practice in your question would be

if (logger.isInfoEnabled()) {
    // do it
}

This is what log4j recommend
ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use org.apache.commons.logging.Log. With this you can log objects (if the level is lower, toString will not be executed).
Yes, Log4J/ Commons logging would be a better option. See this to know why Log4J is better than java.util.logging:

https://www.experts-exchange.com/questions/21728101/java-util-logging.html
Avatar of thesilentkiller
thesilentkiller

ASKER

CEHJ, Ajay-Singh, enachemc, Mayankeagle, thanks for your inputs so far.

I think the discussion is taking a slight turn. I do not want to start a JDK Logging Vs log4j war here :). IMO, I think log4j and JDK logging are mostly similar (yes, there are differences, but they are mostly similar). In course of time, log4j might phase out for the single reason that JDK logging comes part of the JDK, and there are really no strong negative points in the JDK logger.

Coming back to my question, let me re-state it a little bit. Irrespective of whether you use log4j or JDK Logging, do you

(1) Add an 'if' clause for all log statements
(2) Add 'if' clauses for log statements that take up non-trivial processing time
(3) Add 'if' clause for all debug level log statements (since severe and warn usually is turned on even in production time, there is really no need to add an 'if' clause for them)
(4) Do not add 'if' clauses and let the system rot

I am inclined towards (1) since there is no choice for a developer to make. All developers in a team would add 'if' clauses for all log statements. It makes the code a little bulky but it makes the code consistent.

Thanks,
SK
(1) Only on processor intensive toString() functions
(2) True
(3) Would clutter code
(4) Only if you do not have processor intensive toString() functions or the logger knows to log objects (will call itself toString())
>> but they are mostly similar

No, I don't think JDK supports XML configuration. I could be wrong though - maybe they introduced it in 5.0

>> and there are really no strong negative points in the JDK logger

:) if it doesn't support XML configuration, its a negative point - Log4J is perhaps much more *configurable* due to XML support.

>> Add an 'if' clause for all log statements

I personally don't, in Log4J. I let the config-file handle what has to be logged and what not.

>> Add 'if' clauses for log statements that take up non-trivial processing time

No, I turn off what severities of messages I don't want to log, using Log4J.

>> Add 'if' clause for all debug level log statements

No, I just turn them off.

>> Do not add 'if' clauses and let the system rot

Why would the system rot?
You can do the optimizations like:
a. The messages that go to FATAL/ERROR/WARN, should NOT be wrapped around if. These log statements would be logged most of the time, as we run our applications mostly at level WARN/ERROR.

b. The simple statements like:
   Set set = new HashSet(1);
   set.add("abc");
   set.add("xyz");

   logger.debug(set);

   should not be wrapped - doesn't create much overhead. It should NOT be if the set's size is huge.
mayankeagle,

I think you got the question wrong. The question is NOT about what logging level/priority to use. The question is about whether to add an 'if' clause to avoid unnecessary parameter construction. I hope my question description at the beginning of this thread is clear enough!

enachemc, Ajay-Singh,

I guess the consensus is to use the 'if' clause only for debug level log messages (INFO/FINE/FINER/FINEST in JDK logging, DEBUG in log4j), and that too, only if the parameter construction seems costly.

Thanks,
SK
>> I think you got the question wrong.

No.

>> The question is NOT about what logging level/priority to use.

I never said that.

>> The question is about whether to add an 'if' clause to avoid unnecessary parameter construction.

Its not necessary, like I said.