Skip to content

Commit f4dd3a2

Browse files
committed
fix for GRAILS-7015 "Make Grails automatically log request URI and params when an exception is logged by the application"
1 parent c4655a7 commit f4dd3a2

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/java/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolver.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.codehaus.groovy.grails.web.errors;
1717

18+
import java.util.Enumeration;
19+
1820
import grails.util.GrailsUtil;
1921

2022
import javax.servlet.ServletContext;
@@ -69,8 +71,6 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp
6971

7072
GrailsUtil.deepSanitize(ex);
7173

72-
LOG.error(ex.getMessage(), ex);
73-
7474
GrailsWrappedRuntimeException gwrex = new GrailsWrappedRuntimeException(servletContext, ex);
7575
mv.addObject("exception",gwrex);
7676

@@ -82,6 +82,8 @@ public ModelAndView resolveException(HttpServletRequest request, HttpServletResp
8282
// ignore, no app ctx in this case.
8383
}
8484

85+
LOG.error(getRequestLogMessage(request), ex);
86+
8587
if (urlMappings != null) {
8688
UrlMappingInfo info = urlMappings.matchStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
8789
if (info == null) {
@@ -169,4 +171,42 @@ public static RuntimeException getFirstRuntimeException(Throwable e) {
169171
}
170172
return null;
171173
}
174+
175+
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
176+
public static String getRequestLogMessage(HttpServletRequest request) {
177+
StringBuilder sb = new StringBuilder();
178+
179+
sb.append("Exception occurred when processing request: ");
180+
sb.append("[").append(request.getMethod().toUpperCase()).append("] ");
181+
182+
if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
183+
sb.append(request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
184+
} else {
185+
sb.append(request.getRequestURI());
186+
}
187+
188+
Enumeration<String> params = request.getParameterNames();
189+
190+
if(params.hasMoreElements()){
191+
String param;
192+
String values[];
193+
int i;
194+
195+
sb.append(" - parameters:");
196+
197+
while(params.hasMoreElements()){
198+
param = params.nextElement();
199+
values = request.getParameterValues(param);
200+
201+
for(i=0; i< values.length; i++){
202+
sb.append(LINE_SEPARATOR).append(param).append(": ").append(values[i]);
203+
}
204+
}
205+
}
206+
207+
sb.append(LINE_SEPARATOR)
208+
.append("Stacktrace follows:");
209+
210+
return sb.toString();
211+
}
172212
}

src/test/org/codehaus/groovy/grails/web/errors/GrailsExceptionResolverTests.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ class GrailsExceptionResolverTests extends GroovyTestCase {
134134
assertNotNull "should have returned a ModelAndView", modelAndView
135135
assertFalse modelAndView.empty
136136
}
137+
138+
void testLogRequest() {
139+
def request = new MockHttpServletRequest()
140+
request.setRequestURI("/execute/me")
141+
request.setMethod "GET"
142+
request.addParameter "foo", "bar"
143+
request.addParameter "one", "two"
144+
145+
def msg = GrailsExceptionResolver.getRequestLogMessage(request)
146+
147+
assertEquals '''Exception occurred when processing request: [GET] /execute/me - parameters:
148+
foo: bar
149+
one: two
150+
Stacktrace follows:''' , msg
151+
}
137152
}
138153

139154
class DummyViewResolver implements ViewResolver {

0 commit comments

Comments
 (0)