0

I have a piece of java code which catches top level exception like shown below `

public Class myTest {

@Test
public void test() {

  try {
   callSomeMethod()
  } catch (Exception e) {
      LOGGER.error ("Exception occured", e);
      LOGGER.error (e,e);
  }
}

 private callSomeMethod() throws Exception {
   ...
   ...

 }
}

Somehow, I am unable to capture the entire stack trace. Is this due to Logger ? I am using org.apache.log4j.Logger; I tried both versions of Logger method but still entire stack trace is missing. I only get the Exception message. And above method is actually a testng test annotated with @Test `

Thanks in advance for your time.

Entire code for reference as asked by experts below. Please note this worked after I used ByteArrayStream to pipe the stack trace as String per answer given below.

`

@Test 
  public void importLocalCertsOnSecondary () {
  LOGGER.info("Certificate Import URI-" + URIs.IMPORT_LOCAL_CERTIFICATE.getUri());
  try {
    LOGGER.info ("@@ Importing Local Cert for node=>" + TestEnvironment.getIPs().get(0).getIp());
   Util.importCertificates (URIs.IMPORT_LOCAL_CERTIFICATE.getUri(), 
   Constants.IMPORT_LOCAL_CERT_XML.getValue(),
   Constants.DIRECTORY_PATH.getValue(),
   Constants.LOCAL_CERT_CRT_FILE.getValue(),
    Constants.constants.LOCAL_CERT_KEY.getValue(),
    ipAddress,"local");
   } catch (Exception er) {
        ByteArrayOutputStream os = new ByteArrayOutputStream ();
        er.printStackTrace (new PrintStream (os));
        LOGGER.error(new String(os.toByteArray ()));
        //LOGGER.error(er);
        LOGGER.error(" Exception importing certs-"+ er.getLocalizedMessage());
        Assert.fail(new String(os.toByteArray ()));
    }
}

`

Rockoder
  • 746
  • 2
  • 11
  • 22
  • 2
    Did you try: `e.printStackTrace();` ? – Nir Alfasi May 31 '14 at 01:12
  • how did u define your logger variable? can you post your complete code please ? – Kick Buttowski May 31 '14 at 01:19
  • 3
    @KickButtowski: Please don't attempt to advertise your question in another question. It's impolite and distracts from the other question. – Makoto May 31 '14 at 01:23
  • @Makoto it wasn't my intension at all :) but thank you for informing me about it :) – Kick Buttowski May 31 '14 at 01:28
  • See [Get current stack trace in Java](http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java). – PM 77-1 May 31 '14 at 01:30
  • Thanks to those who replied. Yes, I have tried e.printStackTrace() did not work. protected static final Logger LOGGER = Logger.getLogger(myTest.class); – Rockoder Jun 02 '14 at 03:36
  • @Sotirios- I just get java.io.Exception.. with exception trace leading to TestNG but I am not getting the underlying exception trace throw by the code which is called by callSomeMethod(). Basically, I am calling some API and that API is suppose to throw stack trace. Somehow, I don't get to see the whole stack trace. – Rockoder Jun 02 '14 at 03:41

2 Answers2

1

I use this little technique for loggers which only accept a string:

ByteArrayOutputStream os = new ByteArrayOutputStream ();
e.printStackTrace (new PrintStream (os));
LOGGER.error(new String(os.toByteArray ()));
etherous
  • 709
  • 4
  • 10
1

Firstly, I don't think this is caused by the logging framework. The standard PatternLayout is hardwired to print the stacktrace. You can't "configure" it off. (If you used a different layout class, this does not apply. But I assume that you'd know if you did that.)

So why is the stacktrace missing? I can think of two possible reasons:

  • The HotSpot Server JVM has an optimization that uses preallocated exception objects rather than allocating new ones each time. (The saving can be significant.) The downside is that these preallocated exceptions don't record stacktrace information, so if you need the stacktrace for diagnosis, it isn't there.

  • It is also possible to declare an exception to suppress its own stacktrace. What you do is to override the exception's Throwable.fillInStackTrace() method.

References:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216