我正在使用Spring 3.2.11.RELEASE和JBoss 7.1.3.Final。在我的JBoss standalone.xml文件中,我有以下设置
<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
但是,如果我升级级别o 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调试消息的同时,将异常堆栈跟踪显示在我的日志中?
答案 0 :(得分:0)
我知道这已经太晚了,但我自己遇到了这个问题并找到了解决方案,所以我想为什么不在这里分享它,也许它可以帮助其他人将来。
就我而言,在使用Spring Boot 1.4.1.RELEASE
和Spring Web 4.3.3.RELEASE
以及log4j时,DEBUG日志级别下的默认AbstractHandlerExceptionResolver
抛出了异常。因此,只需创建自己的自定义ExceptionResolver
,您可以使用自己的自定义日志级别抛出异常并将其映射到整个Web应用程序。
<强> ApplicationConfiguration.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
}
<强> AppExceptionResolver.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;
}
}