Permalink
Browse files

fix for GRAILS-7015 "Make Grails automatically log request URI and pa…

…rams when an exception is logged by the application"
  • Loading branch information...
1 parent c4655a7 commit f4dd3a2a61fe93e084b21f2f5ea3bcdc4f48fe65 @graemerocher graemerocher committed Dec 14, 2010
@@ -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<String> 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();
+ }
}
@@ -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 {

0 comments on commit f4dd3a2

Please sign in to comment.