Solved

Industry Practice for logging

Posted on 2006-07-21
11
286 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:thesilentkiller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153904
Industry practice is to use log4j
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153929
... and there the sub-practice in your question would be

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

This is what log4j recommend
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 125 total points
ID: 17153933
Its good idea to put the simple messages with logger.info(...);

but the messages that are costly to evaluate (in terms of space and time) should be wrapped around if like you have done above WHEN THE LOGGING LEVEL IS LOWER. It doesn't make more sense to do this if you are logging at error/fatal since most of the applications always runs on error or below
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:enachemc
ID: 17153943
You can use org.apache.commons.logging.Log. With this you can log objects (if the level is lower, toString will not be executed).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17153953
Yes, Log4J/ Commons logging would be a better option. See this to know why Log4J is better than java.util.logging:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21728101.html
0
 

Author Comment

by:thesilentkiller
ID: 17154514
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
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17154561
(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())
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17154597
>> 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?
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17155148
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.
0
 

Author Comment

by:thesilentkiller
ID: 17156405
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17159956
>> 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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 58
swing controls 2 21
web project error add remove 1 51
restrict decimal places for double datatype 10 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question