In special circumstances you might want each line of the stacktrace to be
full log lines (i.e., with loglevel, MDC, threadname, etc).  In this case you can
use Throwable.getStackTrace() to get the individual line, and then do something like

public static void logError(Logger log, Throwable ex, int limit) {
        if (log.isDebugEnabled()) {
                StackTraceElement[] ste = ex.getStackTrace();
                for (int i = 0; i < ste.length; i++) {
                        if (i == limit) {        
                                return;
                        }
                        if (i==0) {
                                log.debug(ste[i]);
                        }
                        else {
                                log.debug("\t" + ste[i]);
                        }
                }
        }
}
 


I use this when I need to see a stacktrace when I "grep" through the log
file for a paricular thread, say.

On Fri, Aug 29, 2003 at 10:28:54AM -0400, Shapira, Yoav wrote:
> 
> Howdy,
> try {
>   String a = null;
>   System.out.println(a.length());
> } catch (Exception e) {
>   logger.error("Error printing string: ", e);
> }
> 
> Note the comma, i.e. the error(Object, Throwable) version of the API.
> All the levels (logger.info(...), logger.warn(...), etc) have a version
> of their signature with a Throwable argument.
> 
> Yoav Shapira
> Millennium ChemInformatics
> 
> 
> >-----Original Message-----
> >From: Andreas Bothner [ MTN - Innovation Centre ]
> >[mailto:[EMAIL PROTECTED]
> >Sent: Friday, August 29, 2003 9:55 AM
> >To: Log4J Users List
> >Subject: how do I log an exception stacktrace
> >
> >Hi,
> >
> >I apologize if this is a stupid question, but how would I log out an
> >exception's stacktrace using logger.debug() ?
> >
> >Regards,
> >Andreas Bothner
> >Systems Integrator
> >
> >
> >NOTE: This e-mail message is subject to the MTN Group disclaimer see
> >http://www.mtn.co.za/email_disclaimer.asp
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 
> 
> This e-mail, including any attachments, is a confidential business communication, 
> and may contain information that is confidential, proprietary and/or privileged.  
> This e-mail is intended only for the individual(s) to whom it is addressed, and may 
> not be saved, copied, printed, disclosed or used by anyone else.  If you are not 
> the(an) intended recipient, please immediately delete this e-mail from your computer 
> system and notify the sender.  Thank you.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-- 
Fergus Gallagher          Tel: +44 (20) 8742 1600
Orbis                     Fax: +44 (20) 8742 2649
414 Chiswick High Street  email: [EMAIL PROTECTED]
London  W4 5TL            Web: http://www.orbisuk.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to