Home > Back-end >  Tried to print stack trace using logback but stack trace is not printing
Tried to print stack trace using logback but stack trace is not printing

Time:04-16

I'm using following code to print any exception occurs within the try catch block, but when the exception occurs logback doesn't print the full stack trace instead it write , a single line of error(which doesn't clearly say what caused it. How can I get the full stack trace printed out in the logback output?

Try catch block that captures the exception

            try {
                // Create JMS objects
                context = cf.createContext();
                destination = context.createQueue("queue:///"   QUEUE_NAME);
                ((MQDestination)destination).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);
            } catch (Exception e) {
                // catch any exception occurred while trying to connect to the destination queue Manager
                e.printStackTrace();
                LOGGER.info(e.toString());
                return "Unable to connect to the destination queue Manager '" QMGR  "'";
            
            }

log back Error out put :

21:18:10.748 [http-nio-8010-exec-4] INFO  com.mqMessageHandler.Webcontroller - com.ibm.msg.client.jms.DetailedJMSRuntimeException: JMSWMQ0018: Failed to connect to queue manager 'KAU.TST' with connection mode 'Client' and host name '192.168.1.25(1540)'.
Check the queue manager is started and if running in client mode, check there is a listener running. Please see the linked exception for more information.

CodePudding user response:

e.toString() will just print the error message.

If you want to print the complete stacktrace , use the error method :

LOGGER.error("Exception occurred",e)

https://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Logger.html#error-java.lang.String-java.lang.Throwable-

If you need to extract out full stack trace , checkout the following util method from apache common :

LOGGER.info("Exception : ",ExceptionUtils.getStackTrace(e));

https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/exception/ExceptionUtils.html#getFullStackTrace(java.lang.Throwable)

  • Related