Solved

Industry Practice for logging

Posted on 2006-07-21
11
278 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
Comment Utility
Industry practice is to use log4j
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
... 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
Comment Utility
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
 
LVL 12

Expert Comment

by:enachemc
Comment Utility
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:mayankeagle
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:thesilentkiller
Comment Utility
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
Comment Utility
(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:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now