Reputation: 2830
I’m using Spring 3.2.11.RELEASE and JBoss 7.1.3.Final. In my JBoss standalone.xml file, I have the following setting
<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>
The problem is the DEBUG level prints out too much stuff that I don’t want, like
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
However, if I upgrade the level o ERROR, I don’t see stack traces from Exceptions thrown iny web app, like
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]
Notice how the first line of the stack trace is Spring’s handler (“org.springframework.web.servlet.DispatcherServlet”), but the actual exception originates from my code (the line with “org.mainco.subco.registration.mvc.RegistrationController”). How can I get the exception stack traces to appear in my log while suppressing all the other non-essential Spring debugging messages?
Upvotes: 1
Views: 12547
Reputation: 6451
I know this is too late, but I have encountered this issue myself and found a solution, so I thought why not share it here, maybe it helps other people out in the future.
In my case, while using Spring Boot 1.4.1.RELEASE
and Spring Web 4.3.3.RELEASE
and log4j, the exceptions were thrown by the default AbstractHandlerExceptionResolver
under the DEBUG log level. So just create your own custom ExceptionResolver
, in which you throw the exception with your own custom log level and map it for the entire web application.
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;
}
}
Upvotes: 0