debugging 如何在日志中打印异常堆栈跟踪,同时抑制其他Spring DEBUG消息?

j0pj023g  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(125)

我使用的是Spring 3.2.11.RELEASE和JBoss 7.1.3.Final。

<subsystem xmlns="urn:jboss:domain:logging:1.1">
        <console-handler name="CONSOLE">
            <level name="DEBUG"/>
            <formatter>
                <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
            </formatter>
        </console-handler>
        <periodic-rotating-file-handler name="FILE">
            <formatter>
                <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
            </formatter>
            <file relative-to="jboss.server.log.dir" path="server.log"/>
            <suffix value=".yyyy-MM-dd"/>
            <append value="true"/>
        </periodic-rotating-file-handler>
        …
        <logger category="org.springframework">
            <level name="DEBUG"/>
        </logger>

问题是DEBUG级别打印出了太多我不想要的东西,比如

15:27:03,753 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] (http-/0.0.0.0:8080-1) Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@5d4c14] after transaction

然而,如果我升级ERROR级别,我看不到异常抛出到web应用程序的堆栈跟踪,比如

15:27:03,696 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-/0.0.0.0:8080-1) Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'activity/response/file'; model is {error={"status":"failure","exception":"NullPointerException"}}: java.lang.NullPointerException
    at org.mainco.subco.registration.mvc.RegistrationController.getInitRegPage(RegistrationController.java:369) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_65]
    at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_65]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.2.11.RELEASE.jar:3.2.11.RELEASE]

请注意,堆栈跟踪的第一行是Spring的处理程序(“org.springframework.web.servlet.DispatcherServlet”),但实际的异常来自我的代码(“org.mainco.subco.registration.mvc.RegistrationController”行)。如何才能使异常堆栈跟踪出现在我的日志中,同时抑制所有其他非必要的Spring调试消息?

wmomyfyw

wmomyfyw1#

我知道这已经太晚了,但我自己也遇到过这个问题,并找到了解决办法,所以我想为什么不在这里分享它,也许它有助于其他人在未来。
在我的例子中,当使用Spring Boot 1.4.1.RELEASESpring Web 4.3.3.RELEASE和log4j时,异常是由DEBUG日志级别下的默认AbstractHandlerExceptionResolver抛出的,所以只需创建您自己的自定义ExceptionResolver,在其中您用自己的自定义日志级别抛出异常,并将其Map到整个Web应用程序。

应用程序配置.java:

@Configuration
    @EnableAutoConfiguration
    @ComponentScan(basePackages = "com.example.app") // in order for the app to scan all components, as well as our custom ExceptionResolver
    public class ApplicationConfiguration
    {
    // database configs & other stuff
    }

应用程序异常解析器.java:

@Component
public class AppExceptionResolver extends AbstractHandlerExceptionResolver
{
    private static final Logger logger = LoggerFactory.getLogger(AppExceptionResolver.class.getSimpleName());

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    {
        logger.error("Application error in: [" + ex.getClass().getName() + "]", ex);
        return null;
    }
}

相关问题