From f4dd3a2a61fe93e084b21f2f5ea3bcdc4f48fe65 Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Tue, 14 Dec 2010 12:26:21 +0100 Subject: [PATCH] fix for GRAILS-7015 "Make Grails automatically log request URI and params when an exception is logged by the application" --- .../web/errors/GrailsExceptionResolver.java | 44 ++++++++++++++++++- .../GrailsExceptionResolverTests.groovy | 15 +++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/java/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolver.java b/src/java/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolver.java index e7ac2d102c8..8dd5fe60c5b 100644 --- a/src/java/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolver.java +++ b/src/java/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolver.java @@ -15,6 +15,8 @@ */ package org.codehaus.groovy.grails.web.errors; +import java.util.Enumeration; + import grails.util.GrailsUtil; import javax.servlet.ServletContext; @@ -69,8 +71,6 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp GrailsUtil.deepSanitize(ex); - LOG.error(ex.getMessage(), ex); - GrailsWrappedRuntimeException gwrex = new GrailsWrappedRuntimeException(servletContext, ex); mv.addObject("exception",gwrex); @@ -82,6 +82,8 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp // ignore, no app ctx in this case. } + LOG.error(getRequestLogMessage(request), ex); + if (urlMappings != null) { UrlMappingInfo info = urlMappings.matchStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); if (info == null) { @@ -169,4 +171,42 @@ public static RuntimeException getFirstRuntimeException(Throwable e) { } return null; } + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + public static String getRequestLogMessage(HttpServletRequest request) { + StringBuilder sb = new StringBuilder(); + + sb.append("Exception occurred when processing request: "); + sb.append("[").append(request.getMethod().toUpperCase()).append("] "); + + if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) { + sb.append(request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE)); + } else { + sb.append(request.getRequestURI()); + } + + Enumeration params = request.getParameterNames(); + + if(params.hasMoreElements()){ + String param; + String values[]; + int i; + + sb.append(" - parameters:"); + + while(params.hasMoreElements()){ + param = params.nextElement(); + values = request.getParameterValues(param); + + for(i=0; i< values.length; i++){ + sb.append(LINE_SEPARATOR).append(param).append(": ").append(values[i]); + } + } + } + + sb.append(LINE_SEPARATOR) + .append("Stacktrace follows:"); + + return sb.toString(); + } } diff --git a/src/test/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolverTests.groovy b/src/test/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolverTests.groovy index 3e8e69e483e..90e80925148 100644 --- a/src/test/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolverTests.groovy +++ b/src/test/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolverTests.groovy @@ -134,6 +134,21 @@ class GrailsExceptionResolverTests extends GroovyTestCase { assertNotNull "should have returned a ModelAndView", modelAndView assertFalse modelAndView.empty } + + void testLogRequest() { + def request = new MockHttpServletRequest() + request.setRequestURI("/execute/me") + request.setMethod "GET" + request.addParameter "foo", "bar" + request.addParameter "one", "two" + + def msg = GrailsExceptionResolver.getRequestLogMessage(request) + + assertEquals '''Exception occurred when processing request: [GET] /execute/me - parameters: +foo: bar +one: two +Stacktrace follows:''' , msg + } } class DummyViewResolver implements ViewResolver {