• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logging Throwables Java 5  RSS feed

 
Ryan Day
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some custom Exceptions that I construct by passing a string and another Throwable. For example, I get a database error and I wrap it with a custom business exception.

I would like to add a log() method in my custom Exceptions, which will log information about the Exception, and then traverse all the throwables it is wrapped around.

Sounds simple but I'm getting stuck. What's the best way to do this?
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,

Can you elaborate where are you getting stuck?
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan,

One of the industry standards for logging in Java applications is Apache's log4j component. It can be a little tedious to set up if you have never used it before. You may also want to consider looking into Java's Logger class. It is a simpler API but similar to log4j.

Hope this helps,
Jeff
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Day:
I would like to add a log() method in my custom Exceptions, which will log information about the Exception, and then traverse all the throwables it is wrapped around.


This sounds unwise. Exceptions should not do logging. They should contain the data pertinent to the exception. They may contain method(s) to convert themselves to a human-readable string, so that some other class can log them.

Also, you probably shouldn't be using Throwable here, because that implies you might be catching Errors somewhere. That's almost always wrong, as Errors indicate serious unrecoverable JVM-wide problems, for which the only solution is usually exiting the whole process (perhaps after attempting some minimal clean-up).
[ June 04, 2008: Message edited by: Peter Chase ]
 
Ryan Day
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help everyone. Your advice helped me clarify my strategy on this one.
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you have used apache log4j then follow this pattern (which I found very effective in my professional work) :



hope it helps.
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Sanjay

Sorry, but I wouldn't catch an exception and then throw a new one of the same type without wrapping the original one, since you might be losing important information about the exception root cause.

Also, log4j API has debugging levels, so you can control how much is being logged for your application. That goes from debug, info, warning, to error, etc. I would use warning or error for the logging of exceptions.

So, instead of:



you could either do:



or just:



Of course, your code works fine, just small enhancements
 
sanjay kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm aware of the logging levels.

i would have used


or
what you have said.

since I'm passing the error from my server side code to the client side I'm throwing a new exception

Though constructive criticism is always welcome and for everyone's good @rodrigo
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rodrigo Tomita:


or just:


I'd turn it around - log first, throw second. Otherwise the code won't compile - the exception will cause the logging code never to be reached
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Peter Chase]: Also, you probably shouldn't be using Throwable here, because that implies you might be catching Errors somewhere. That's almost always wrong, as Errors indicate serious unrecoverable JVM-wide problems, for which the only solution is usually exiting the whole process (perhaps after attempting some minimal clean-up).

Eh, I don't really buy that. Yes, I agree that usually, catching an Error is a bad idea - but not always. The case against And if you create a new Exception that can't wrap a Throwable, but only an Exception, you're preventing people from ever using it to catch and re-wrap an error. I think that's a mistake.

Well, actually you don't completely prevent it, since they can always use the initCause() method inherited from Throwable. That's a bit more cumbersome though, and often overlooked.

The issue of catching Errors has been discussed numerous times here. These are some of my past diatribes on the subject:

http://www.coderanch.com/t/409600/java/java/catching-Throwable
http://www.coderanch.com/t/409350/java/java/extra-Exception-catching
http://www.coderanch.com/t/328927/java/java/catch-Throwable-or-not
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the original question here: generally, when I catch an exception, I would not log it and re-throw it. That just leads to the same exception being logged multiple times in the log files. I prefer to either log the exception, or re-throw it. Logging the exception is the responsibility of the last handler, whichever one does not re-throw the exception. Of course, that assumes that the final handler will indeed log the exception. If that last handler is written incorrectly (such that the exception is not logged at all), you may need to implement catch-and-rethrow in other handlers to help in your debugging. Then once you track down the author of the final handler that failed to log the exception, shoot them.
[ June 07, 2008: Message edited by: Jim Yingst ]
 
Rodrigo Tomita
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

I'd turn it around - log first, throw second. Otherwise the code won't compile - the exception will cause the logging code never to be reached


Woops, my bad :roll:
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use the java.util.logging classes?
They are already there and you don't need an additional package in your application.

Originally posted by Jeff Storey:
Ryan,

One of the industry standards for logging in Java applications is Apache's log4j component. It can be a little tedious to set up if you have never used it before. You may also want to consider looking into Java's Logger class. It is a simpler API but similar to log4j.

Hope this helps,
Jeff
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodrigo,

While java.util.Logging is already built in, Apache log4j does offer a bit more flexibility and more features. Here's an article on some of the differences: http://java.sys-con.com/read/48541.htm. For some cases, java.util.Logging may very well be appropriate and sufficient. But, many 3rd party libraries (specifically open source) use log4j, and by using it, you can also more easily integrate with their logging.

Jeff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!