aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Proper Use of Logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Proper Use of Logging" Watch "Proper Use of Logging" New topic
Author

Proper Use of Logging

Marcelo Ruiz
Greenhorn

Joined: Dec 19, 2007
Posts: 25
Hi All!

I have a question related to the proper use of logging. Maybe somebody will clarify the situation for me.

Please consider the following scenario, where three classes (each of them produce logging) are involved to allow an user to (lets say) send an e-mail. Suppose all of them have a method called "void sendMail", but the method in Class C is where the functionality is implemented: class A delegates to class B which in turn delegates to class C.

Now, lets suppose I want to add logging to my program. My question problem is deciding "What and where to log".
The following are the options I considered (along with the reasoning for each one):

Class A -------------> Class B -------------> Class C (sends the e-mail)


Option #1
---------
Class A:
entering
exiting

Class B:
entering
exiting

Class C:
entering
info (mail sent)
exiting

Reasoning: the important action happens in Class C, so log the info message just there.


Option #2
---------
Class A:
entering
info (mail sent)
exiting

Class B:
entering
info (mail sent)
exiting

Class C:
entering
info (mail sent)
exiting

Reasoning: someone reading the log of Class A should know the action happened, and that person does not care how many chained low level calls were made.


Option #3
---------
Class A:
entering
info (mail sent)
exiting

Class B:
entering
fine (mail sent)
exiting

Class C:
entering
finer (mail sent)
exiting

Reasoning: the info log should be logged at the highest level class, and the lower level classes should log "less important" messages in case tracking all the calls is necessary.


Option #4
---------
Class A:
entering
info (mail sent)
exiting

Class B:
entering
exiting

Class C:
entering
exiting

Reasoning: the info log should be logged at the highest level class, and the lower level classes are just helper classes that should not produce any log except (except for entering/exiting/arguments/returning).


I guess the same could be ask for an exceptions being thrown. So I am basically trying to get a best practice advice (I guess there should be a best practice guideline for logging somewhere... or maybe there is a book or an article about this? :S I could only find on-line logging tutorials that teach how to use the API and the reasoning for using the different logging Levels, but no explanation about how to solve this problem).
I would appreciate to hear which option (they might be more I just didn't think about, so please feel free to add them) would you choose and why.


Thank you all!

Marcelo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Marcelo,

if this is a question related to the SCJD, then the answer is quiet simple: logging is not required by the assignment, so you don't need to add it. If this is not related to SCJD you have posted your question on the wrong forum.

For the SCJD I used myself a bit of logging: entering and exiting logging in each method of the Data class and my business service implementations. Main purpose: debugging when the class has to be extended or some changes have to be made, so my debugging messages can be reused. I turned logging off when submitting my assignment.

Back to your initial request: I don't see any added value in adding logging messages to classes that are just delegating calls to another class. If they only delegate it has completely no added value. If in 1 of the delegating classes some adjustment/changes with the parameters happen (before passing it to the method doing all the business) then adding log messages could be done.
The same applies for exceptions: it has no added value to log the exception in class A, class B and class C. If you log your exception appropriately you will have your stack trace to determine the execution path. So you log your level at the lowest level (class C) if that's the place where you handle the exception. I don't think it's a good idea to just catch the exception to log it and then just rethrow it. If you only catch the exception in class A (class B and C just declare throwing the exception) I would log it there.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Marcelo Ruiz
Greenhorn

Joined: Dec 19, 2007
Posts: 25
Hi Roel,

Thanks for your answer.
My question is related to the SCJD. I have Andrew's book and he uses logging in the example program.
I was wondering if, after deciding using logging, I would get into trouble for not following (possibly existent) logging best practices. I am surprised nothing of that is published somewhere (especially after considering the detail of Sun's code conventions).
I guess I'll stick to option #1 unless someone else points me to another direction!
Again, thanks for your answer.

Marcelo.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

Hi Marcelo,

This is my thoughts about use of logging during the assignment. First I was not going to add it, because it is not required by the instructions. So to get some debug information, I simply used some System.out.println() statements. These statements would be removed when submitting my assignment.
Afterwards I thought just changing these System.out.println() statements to just using a Logger instead of standard output. The Logger just used a ConsoleHandler (so not a lot difference with the sysouts) and added a note about it in my choices.txt

I know for example that the great Brasilian SCJD guru Roberto Perillo didn't provide any logging in his assignment. He used sysouts which were deleted when he submitted his assignment, so it certainly is not necessary to add logging to pass this assignment.

Kind regards,
Roel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper Use of Logging