Wednesday, January 12, 2011

Spring log not showing Runtime Exceptions

I had to increase log level to DEBUG to find out that Spring was swallowing an important SQLException.

Commonly you use com.nestorurquiza.web.handler.SimpleMappingExceptionResolver but the exception gets logged with DEBUG level instead.

To solve this problem I extended the class with the code shown below:
public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver{
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex) {
        final Logger log = LoggerFactory.getLogger(CustomSimpleMappingExceptionResolver.class);
        log.error(Utils.stack2string(ex));
        return super.resolveException(request, response, handler, ex);
    }
}

I have to say it again application monitoring should be done from outside and never from inside. Swallowing the exception to rely on things like an SMTP appender and so on break that statement. If email fails and you are dealing with external users most likely the user will complain about a problem you are simply unable to see on your back end. Be aware!

No comments:

Followers