• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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