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 ()));
}
}
`