Skip to content

Commit

Permalink
fix for GRAILS-7015 "Make Grails automatically log request URI and pa…
Browse files Browse the repository at this point in the history
…rams when an exception is logged by the application"
  • Loading branch information
graemerocher committed Dec 14, 2010
1 parent c4655a7 commit f4dd3a2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Expand Up @@ -15,6 +15,8 @@
*/
package org.codehaus.groovy.grails.web.errors;

import java.util.Enumeration;

import grails.util.GrailsUtil;

import javax.servlet.ServletContext;
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
Expand Up @@ -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 {
Expand Down

0 comments on commit f4dd3a2

Please sign in to comment.