摆脱 Tomcat 中的 Spring 日志

Posted

技术标签:

【中文标题】摆脱 Tomcat 中的 Spring 日志【英文标题】:Getting rid of Spring logs in Tomcat 【发布时间】:2013-02-12 03:14:31 【问题描述】:

我正在为在 Tomcat 7 上运行的 Java Web 应用程序使用 spring-mvc 和安全性。我通过扩展 AbstractAuthenticationProcessingFilter 自定义了 spring 安全性并捕获自定义异常,我有一个扩展 ExceptionHandlerExceptionResolver 的类。

现在,当我遇到异常时,无论是否在运行时,我都无法在我的 tomcat 日志中看到异常的原因。 Tomcat 会生成一个日志,该日志是关于 spring 安全链中的类和 ExceptionHandlerExceptionResolver 类的。

如何取回我的默认日志?

示例日志:

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:150) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) com.cagatay.deneme.server.security.AuthFilter.doFilter(AuthFilter.java:65) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

根本原因 java.lang.NullPointerException com.cagatay.deneme.server.controller.errorhandler.ErrorController.doResolveHandlerMethodException(ErrorController.java:40) org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:60) org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136) org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1148) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:985) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:150) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) com.cagatay.deneme.server.security.AuthFilter.doFilter(AuthFilter.java:65) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

【问题讨论】:

【参考方案1】:

根据堆栈跟踪,您似乎在com.cagatay.deneme.server.controller.errorhandler.ErrorController 的第 40 行遇到了 NullPointerException 的问题。您需要找出导致此问题的原因并进行修复。

编辑:

根据docs for AbstractHandlerMethodExceptionResolver,异常处理程序应该返回ModelAndView。在您的代码中,会抛出 NullPointerException。现在不看你的代码,我不知道你是否重新抛出异常,但堆栈跟踪让我相信 NPE 起源于第 40 行。恕我直言,异常处理程序本身不应该抛出异常。

【讨论】:

不,这只是一个示例,错误出现在另一个类中。 ErrorController 是我从 ExceptionHandlerExceptionResolver 扩展类的名称。 感谢更新 beny,但您看到我的主要问题是我的日志中不相关的数据。在上面的示例中,spring security 填充了整个堆栈跟踪。 但是堆栈跟踪是相关的,因为它指向 ErrorController 中的错误... 恐怕你错了,Errorcontroller 是扩展 ExceptionResolver 的类,异常被抛出到其他地方并被捕获,这就是为什么日志是关于它的,我的抱怨是关于这个案例。 如果异常是在其他地方抛出的,它不会在ErrorController 的第 40 行显示root cause: NullPointerException。我会设置一个断点调试只是为了仔细检查......【参考方案2】:

在***别,您可以捕获任何预期的和意外的异常,并在您希望它们出现时自行手动记录它们,而不是让它们传播到 Spring 并让它自己做。

一种方法是使用 ExceptionHandler。示例:http://www.mkyong.com/spring-mvc/spring-mvc-exceptionhandler-example/

【讨论】:

以上是关于摆脱 Tomcat 中的 Spring 日志的主要内容,如果未能解决你的问题,请参考以下文章

独立tomcat中的Spring Boot日志文件以XML格式保存

Spring Boot自定义的Tomcat打印catalina和tomcat日志

spring boot .war tomcat 应用程序日志不存在

你如何摆脱 ConfigurationPropertiesRebinderAutoConfiguration INFO 日志消息

如何利用Spring Boot框架开发一个全球化的应用程序

Spring Boot – Jetty配置