首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试使用logback打印堆栈跟踪,但堆栈跟踪没有打印

尝试使用logback打印堆栈跟踪,但堆栈跟踪没有打印
EN

Stack Overflow用户
提问于 2022-04-15 20:24:40
回答 1查看 1.3K关注 0票数 1

我使用下面的代码来打印try catch块中发生的任何异常,但是当异常发生时,logback并不会打印完整的堆栈跟踪,而是只会写入一行错误(这不能清楚地说明是什么导致的)。如何在logback输出中打印出完整的堆栈跟踪?

尝试捕捉异常的catch块

代码语言:javascript
运行
复制
            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 +"'";
            
            }

日志返回错误输出:

代码语言:javascript
运行
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-15 22:18:52

e.toString()将只打印错误消息。

如果要打印完整的堆栈跟踪,请使用错误方法:

代码语言:javascript
运行
复制
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-

如果需要提取完整的堆栈跟踪,请从ApacheCommon中签出以下util方法:

代码语言:javascript
运行
复制
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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71888635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档