• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

Can I send the debug message using printStackTrace()

What I know is when I use printStackTrace() it needs to catch the exception such as

  Try
  {
   
  }
    catch (InterruptedException ie) {
     ie.printStackTrace();                       <=== this one is send to the default log file, report.log
      }


My question is I send the debug message to report.log such as

Try
{
   printStackTrace("Update General Ledger"    <===== can i do this. IF NOT, any way i can do.
}


PLEASE ADVISE
0
LeanMoreTryMore
Asked:
LeanMoreTryMore
  • 7
  • 3
  • 3
  • +1
2 Solutions
 
objectsCommented:
do it in two lines:

System.err.println("Update General Ledger");
ie.printStackTrace();
0
 
objectsCommented:
log4j would allow you to do it using:

log.warn("Update General Ledger" , ie);
0
 
aozarovCommented:
You can use the following static method, stackTraceToString, to capture stackTrace to String
and then you can do whatever you want with that string like logging it to a file...

import java.io.*;

public class CaptureStackTrace
{
      public static void main(String st[])
      {
            Exception ex = new Exception();
            String exStackTrace = stackTraceToString(ex);
            System.out.println("My Exception\n" + exStackTrace);
      }

      public static String stackTraceToString(Exception ex)
      {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            pw.flush();
            return sw.toString();
      }
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
limaidealCommented:
Exception.printStachTrace() actually print out to stderr outputstream. For the seam reason, if you want to output things other than Excception to output log, you can use System.err.println().
0
 
objectsCommented:
> to capture stackTrace to String

Thats not necessary
0
 
aozarovCommented:
>> Thats not necessary
What a generic statement it is...
That can be usefull in many cases including if want to log the stackTrace to a specific log file. (and you are not using log4j).

>> ie.printStackTrace();
As limaideal said the output for this command will go to stderr. if you want to direct it to stdout you can do
ie.printStackTrace(System.out);
0
 
objectsCommented:
If you want to log to a different stream, just direct the trace to that stream. Theres no need to write it to a string.

writer.println("Update General Ledger");
io.printStackTrace(writer);
0
 
objectsCommented:
or just redirect stderr if you want all stderr to go to a different stream.
0
 
LeanMoreTryMoreAuthor Commented:
what is log4j? how do i use it?
0
 
LeanMoreTryMoreAuthor Commented:
System.err.println("Update General Ledger");
ie.printStackTrace();

I tried the above, but NOT WORKING.



does not recongize ie. this only goes to exception. My idea is to add a trace to the log file, report.log that is system generated to capture the stderr.

For example,

             System.out.println("Process Start " + processDateTime() + " - " + eventQueue._executeCmdString);

I want to add the above standard output to stderr, report.log, If i do, System.err.println then ie.printStackTrace,
ie.printStackTrace(), i got compliation error as it does not recognize ie.

              // Execute command and wait for completion.
              deleteChildProcess(eventQueue._jr_no);
0
 
LeanMoreTryMoreAuthor Commented:
I want to use log4j it seems to be very useful. When I try to import the following, i got error.
Would anyone tell me where to get the jar file which contains log4j


import org.apache.log4j.*;

0
 
objectsCommented:
0
 
aozarovCommented:
>> If you want to log to a different stream, just direct the trace to that stream.
I actually showed that way at the same comment but your File stream might not always be avaible to the one that handles this exception.
And as I said there are many other usage examples like adding this information to DB, sending it as part of your email, ...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now