Skip to content

Commit dca3b58

Browse files
committed
Formatting request parameters and log information
The excluded parameters will be marked [FILTERED] in the log.
1 parent 88b8535 commit dca3b58

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

grace-bootstrap/src/main/groovy/org/grails/exceptions/reporting/DefaultStackTraceFilterer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,20 @@ public DefaultStackTraceFilterer(boolean shouldFilter) {
7272
this.packagesToFilter.addAll(Arrays.asList(DEFAULT_INTERNAL_PACKAGES));
7373
}
7474

75+
@Override
7576
public void addInternalPackage(String name) {
7677
if (name == null) {
7778
throw new IllegalArgumentException("Package name cannot be null");
7879
}
7980
this.packagesToFilter.add(name);
8081
}
8182

83+
@Override
8284
public void setCutOffPackage(String cutOffPackage) {
8385
this.cutOffPackage = cutOffPackage;
8486
}
8587

88+
@Override
8689
public Throwable filter(Throwable source, boolean recursive) {
8790
if (recursive) {
8891
Throwable current = source;
@@ -95,6 +98,7 @@ public Throwable filter(Throwable source, boolean recursive) {
9598
return filter(source);
9699
}
97100

101+
@Override
98102
public Throwable filter(Throwable source) {
99103
if (this.shouldFilter) {
100104
StackTraceElement[] trace = source.getStackTrace();
@@ -153,8 +157,14 @@ protected boolean isApplicationClass(String className) {
153157
return true;
154158
}
155159

160+
@Override
156161
public void setShouldFilter(boolean shouldFilter) {
157162
this.shouldFilter = shouldFilter;
158163
}
159164

165+
@Override
166+
public boolean isShouldFilter() {
167+
return this.shouldFilter;
168+
}
169+
160170
}

grace-bootstrap/src/main/groovy/org/grails/exceptions/reporting/StackTraceFilterer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2022 the original author or authors.
2+
* Copyright 2011-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,9 @@
1818
/**
1919
* Improves the output of stack traces produced by exceptions in a Grails application.
2020
*
21-
* @since 2.0
2221
* @author Graeme Rocher
22+
* @author Michaael Yan
23+
* @since 2.0
2324
*/
2425
public interface StackTraceFilterer {
2526

@@ -62,4 +63,9 @@ public interface StackTraceFilterer {
6263
*/
6364
void setShouldFilter(boolean shouldFilter);
6465

66+
/**
67+
* @return boolean Whether to filter stack traces or not
68+
*/
69+
boolean isShouldFilter();
70+
6571
}

grace-web-mvc/src/main/groovy/org/grails/web/errors/GrailsExceptionResolver.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* Wraps any runtime exceptions with a GrailsWrappedException instance.
5858
*
5959
* @author Graeme Rocher
60+
* @author Michael Yan
6061
*/
6162
@SuppressWarnings({ "rawtypes", "unchecked" })
6263
public class GrailsExceptionResolver extends SimpleMappingExceptionResolver implements ServletContextAware, GrailsApplicationAware {
@@ -263,7 +264,8 @@ protected String getRequestLogMessage(String exceptionName, HttpServletRequest r
263264

264265
sb.append(exceptionName)
265266
.append(" occurred when processing request: ")
266-
.append("[").append(request.getMethod().toUpperCase()).append("] ");
267+
.append(LINE_SEPARATOR);
268+
sb.append("URI: ");
267269

268270
if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
269271
sb.append(request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
@@ -272,6 +274,13 @@ protected String getRequestLogMessage(String exceptionName, HttpServletRequest r
272274
sb.append(request.getRequestURI());
273275
}
274276

277+
sb.append(LINE_SEPARATOR);
278+
sb.append("Method: ").append(request.getMethod().toUpperCase()).append(LINE_SEPARATOR);
279+
280+
if (message != null) {
281+
sb.append("Message: ").append(message).append(LINE_SEPARATOR);
282+
}
283+
275284
Config config = this.grailsApplication != null ? this.grailsApplication.getConfig() : null;
276285
boolean shouldLogRequestParameters = config != null ? config.getProperty(Settings.SETTING_LOG_REQUEST_PARAMETERS,
277286
Boolean.class, Environment.getCurrent() == Environment.DEVELOPMENT) : false;
@@ -282,9 +291,8 @@ protected String getRequestLogMessage(String exceptionName, HttpServletRequest r
282291
if (params.hasMoreElements()) {
283292
String param;
284293
String[] values;
285-
int i;
286294

287-
sb.append(" - parameters:");
295+
sb.append("Parameters:");
288296

289297
List<String> blackList = (config.getProperty(Settings.SETTING_EXCEPTION_RESOLVER_PARAM_EXCLUDES,
290298
List.class, Collections.emptyList()));
@@ -296,28 +304,33 @@ protected String getRequestLogMessage(String exceptionName, HttpServletRequest r
296304
param = params.nextElement();
297305
values = request.getParameterValues(param);
298306

299-
if (values != null) {
300-
for (i = 0; i < values.length; i++) {
301-
sb.append(LINE_SEPARATOR).append(param).append(": ");
307+
String paramName = param;
308+
if (paramName.endsWith("[]")) {
309+
paramName = paramName.substring(0, paramName.length() - 2);
310+
}
302311

303-
if (blackList.contains(param)) {
304-
sb.append("***");
305-
}
306-
else {
307-
sb.append(values[i]);
308-
}
312+
sb.append(LINE_SEPARATOR).append(" - ").append(paramName).append(": ");
313+
if (values != null && values.length > 0) {
314+
if (blackList.contains(paramName)) {
315+
sb.append("[FILTERED]");
316+
}
317+
else {
318+
sb.append(String.join(", ", values));
309319
}
310320
}
311321
}
322+
323+
sb.append(LINE_SEPARATOR);
312324
}
313325
}
314326

315327
sb.append(LINE_SEPARATOR);
316-
if (message != null) {
317-
sb.append(message).append(". ");
328+
if (this.stackFilterer.isShouldFilter()) {
329+
sb.append("Filtered stacktrace:");
330+
}
331+
else {
332+
sb.append("Full stacktrace:");
318333
}
319-
sb.append(LINE_SEPARATOR);
320-
sb.append("Stacktrace follows:");
321334

322335
return sb.toString();
323336
}

0 commit comments

Comments
 (0)