Solved

Industry Practice for logging

Posted on 2006-07-21
11
285 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
object oriented programming comparison 5 77
servlet example 11 49
expectj telnet failing 5 45
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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