Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Need a logging package that will log the stacktrace

Thread view: 
Dean Schulze - 17 Jan 2006 16:25 GMT
Is there a Logger that will easily log my stacktrace (what I get with
ex.printStackTrace())?  I've searched the log4j docs and found nothing
about logging the stacktrace.

I can do this myself with

    StringWriter sw = new StringWriter();
    ex.printStackTrace(new PrintWriter(sw));
    String stacktrace = sw.toString();
    logger.error(stacktrace);

but I don't want to duplicate this code all over the place.

If log4j won't do this for me is there another logging package that will
 log the stacktrace for me?

Thanks.
Joe Attardi - 17 Jan 2006 16:40 GMT
Hey Dean,

Worst case, you could just have a helper class with a static method to
log the stack trace. It could be something like

public class ExceptionLogger {
   public static void logException(Logger logger, Throwable ex) {
           StringWriter sw = new StringWriter();
           ex.printStackTrace(new PrintWriter(sw));
           String stacktrace = sw.toString();
           logger.error(stacktrace);
   }
}

then in your code wherever you need to log it, just call
ExceptionLogger.logException(logger, ex);

What do you think of that approach?

-- Joe Attardi
Dean Schulze - 17 Jan 2006 17:06 GMT
That will cut down on code duplication.

I'm surprised that the standard and defacto standard logging APIs don't
include the stacktrace.  I thought there'd be one out there that did this.

> Hey Dean,
>
[quoted text clipped - 16 lines]
>
> -- Joe Attardi
Daniel Dyer - 17 Jan 2006 17:21 GMT
> That will cut down on code duplication.
>
> I'm surprised that the standard and defacto standard logging APIs don't  
> include the stacktrace.  I thought there'd be one out there that did  
> this.

If you look at the Log4J API docs you will see that each of the trace,  
debug, info, warn, error and fatal methods are overloaded with a version  
that takes two parameters, the second parameter being a Throwable.  These  
overloaded methods will output the stack trace from that Throwable along  
with the message that you pass in the first parameter.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Patrick May - 17 Jan 2006 21:33 GMT
> > I'm surprised that the standard and defacto standard logging APIs
> > don't include the stacktrace.  I thought there'd be one out there
[quoted text clipped - 6 lines]
> from that Throwable along with the message that you pass in the
> first parameter.

    The java.util.logging.Logger class has this capability as well if
you use the log(Level,String,Throwable) method instead of the
convenience methods.

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc.    | The experts in large scale distributed OO
                        | systems design and implementation.
         pjm@spe.com    | (C++, Java, Common Lisp, Jini, CORBA, UML)
Dean Schulze - 18 Jan 2006 00:16 GMT
That worked.  Thanks.

I knew I had seen stack traces in .log files, but their API isn't very
intuitive.

>> That will cut down on code duplication.
>>
[quoted text clipped - 10 lines]
>
> Dan.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.