java - 尝试使用 logback 打印堆栈跟踪,但堆栈跟踪未打印

标签 java spring-boot logback

我使用以下代码来打印 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)

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

如果您需要提取完整的堆栈跟踪,请从 apache common 查看以下 util 方法:

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)

关于java - 尝试使用 logback 打印堆栈跟踪,但堆栈跟踪未打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71888635/

相关文章:

java - gradle 依赖项 : what "classpath" keyword means exactly and where it is documented?

java - 为什么对静态成员和方法使用公共(public)和私有(private)?

javascript - 如何从 HTML 页面解析隐藏的 Javascript 部分

java - 如何在自定义数组列表中找到数组的索引...?

spring - 如何在 Spring Batch 中分别读取平面文件头和正文

postgresql - hibernate不创建表postgresql

java - 如何编写非封装单元测试?

tomcat - 部署为 .war 时的 Spring-Boot Logging 配置

logback - JBoss AS7 : logging with logback

java - Eclipse 在我的单元测试类路径中找不到 logback-test.xml