我使用以下代码来打印 try catch block 中发生的任何异常,但是当异常发生时 logback 不会打印完整的堆栈跟踪,而是写入一行错误(没有明确说明)是什么导致了它。如何在 logback 输出中打印出完整的堆栈跟踪?
try catch 异常的 catch block
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 +"'";
}
登录错误输出:
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.
最佳答案
e.toString()
将只打印错误消息。
如果您想打印完整的堆栈跟踪,请使用错误方法:
LOGGER.error("Exception occurred",e)
如果您需要提取完整的堆栈跟踪,请从 apache common 查看以下 util 方法:
LOGGER.info("Exception : ",ExceptionUtils.getStackTrace(e));
关于java - 尝试使用 logback 打印堆栈跟踪,但堆栈跟踪未打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71888635/