Skip to content

Commit 6d7612e

Browse files
committed
AbstractTraceInterceptor provides logExceptionStackTrace flag and writeToLog delegates
Issue: SPR-15763 (cherry picked from commit aa0d7a6)
1 parent ed5cc27 commit 6d7612e

File tree

6 files changed

+68
-38
lines changed

6 files changed

+68
-38
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractMonitoringInterceptor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525
/**
2626
* Base class for monitoring interceptors, such as performance monitors.
27-
* Provides {@code prefix} and {@code suffix} properties
28-
* that help to classify/group performance monitoring results.
27+
* Provides configurable "prefix and "suffix" properties that help to
28+
* classify/group performance monitoring results.
2929
*
30-
* <p>Subclasses should call the {@code createInvocationTraceName(MethodInvocation)}
31-
* method to create a name for the given trace that includes information about the
32-
* method invocation under trace along with the prefix and suffix added as appropriate.
30+
* <p>In their {@link #invokeUnderTrace} implementation, subclasses should call the
31+
* {@link #createInvocationTraceName} method to create a name for the given trace,
32+
* including information about the method invocation along with a prefix/suffix.
3333
*
3434
* @author Rob Harrop
3535
* @author Juergen Hoeller

spring-aop/src/main/java/org/springframework/aop/interceptor/AbstractTraceInterceptor.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public abstract class AbstractTraceInterceptor implements MethodInterceptor, Ser
6060
*/
6161
private boolean hideProxyClassNames = false;
6262

63+
/**
64+
* Indicates whether to pass an exception to the logger.
65+
* @see #writeToLog(Log, String, Throwable)
66+
*/
67+
private boolean logExceptionStackTrace = true;
68+
6369

6470
/**
6571
* Set whether to use a dynamic logger or a static logger.
@@ -99,6 +105,17 @@ public void setHideProxyClassNames(boolean hideProxyClassNames) {
99105
this.hideProxyClassNames = hideProxyClassNames;
100106
}
101107

108+
/**
109+
* Set whether to pass an exception to the logger, suggesting inclusion
110+
* of its stack trace into the log. Default is "true"; set this to "false"
111+
* in order to reduce the log output to just the trace message (which may
112+
* include the exception class name and exception message, if applicable).
113+
* @since 4.3.10
114+
*/
115+
public void setLogExceptionStackTrace(boolean logExceptionStackTrace) {
116+
this.logExceptionStackTrace = logExceptionStackTrace;
117+
}
118+
102119

103120
/**
104121
* Determines whether or not logging is enabled for the particular {@code MethodInvocation}.
@@ -172,6 +189,40 @@ protected boolean isLogEnabled(Log logger) {
172189
return logger.isTraceEnabled();
173190
}
174191

192+
/**
193+
* Write the supplied trace message to the supplied {@code Log} instance.
194+
* <p>To be called by {@link #invokeUnderTrace} for enter/exit messages.
195+
* <p>Delegates to {@link #writeToLog(Log, String, Throwable)} as the
196+
* ultimate delegate that controls the underlying logger invocation.
197+
* @since 4.3.10
198+
* @see #writeToLog(Log, String, Throwable)
199+
*/
200+
protected void writeToLog(Log logger, String message) {
201+
writeToLog(logger, message, null);
202+
}
203+
204+
/**
205+
* Write the supplied trace message and {@link Throwable} to the
206+
* supplied {@code Log} instance.
207+
* <p>To be called by {@link #invokeUnderTrace} for enter/exit outcomes,
208+
* potentially including an exception. Note that an exception's stack trace
209+
* won't get logged when {@link #setLogExceptionStackTrace} is "false".
210+
* <p>By default messages are written at {@code TRACE} level. Subclasses
211+
* can override this method to control which level the message is written
212+
* at, typically also overriding {@link #isLogEnabled} accordingly.
213+
* @since 4.3.10
214+
* @see #setLogExceptionStackTrace
215+
* @see #isLogEnabled
216+
*/
217+
protected void writeToLog(Log logger, String message, @Nullable Throwable ex) {
218+
if (ex != null && this.logExceptionStackTrace) {
219+
logger.trace(message, ex);
220+
}
221+
else {
222+
logger.trace(message);
223+
}
224+
}
225+
175226

176227
/**
177228
* Subclasses must override this method to perform any tracing around the
@@ -181,13 +232,15 @@ protected boolean isLogEnabled(Log logger) {
181232
* <p>By default, the passed-in {@code Log} instance will have log level
182233
* "trace" enabled. Subclasses do not have to check for this again, unless
183234
* they overwrite the {@code isInterceptorEnabled} method to modify
184-
* the default behavior.
235+
* the default behavior, and may delegate to {@code writeToLog} for actual
236+
* messages to be written.
185237
* @param logger the {@code Log} to write trace messages to
186238
* @return the result of the call to {@code MethodInvocation.proceed()}
187239
* @throws Throwable if the call to {@code MethodInvocation.proceed()}
188240
* encountered any errors
189-
* @see #isInterceptorEnabled
190241
* @see #isLogEnabled
242+
* @see #writeToLog(Log, String)
243+
* @see #writeToLog(Log, String, Throwable)
191244
*/
192245
protected abstract Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable;
193246

spring-aop/src/main/java/org/springframework/aop/interceptor/CustomizableTraceInterceptor.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -276,29 +276,6 @@ protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throw
276276
}
277277
}
278278

279-
/**
280-
* Writes the supplied message to the supplied {@code Log} instance.
281-
* @see #writeToLog(org.apache.commons.logging.Log, String, Throwable)
282-
*/
283-
protected void writeToLog(Log logger, String message) {
284-
writeToLog(logger, message, null);
285-
}
286-
287-
/**
288-
* Writes the supplied message and {@link Throwable} to the
289-
* supplied {@code Log} instance. By default messages are written
290-
* at {@code TRACE} level. Sub-classes can override this method
291-
* to control which level the message is written at.
292-
*/
293-
protected void writeToLog(Log logger, String message, @Nullable Throwable ex) {
294-
if (ex != null) {
295-
logger.trace(message, ex);
296-
}
297-
else {
298-
logger.trace(message);
299-
}
300-
}
301-
302279
/**
303280
* Replace the placeholders in the given message with the supplied values,
304281
* or values derived from those supplied.

spring-aop/src/main/java/org/springframework/aop/interceptor/JamonPerformanceMonitorInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -121,7 +121,7 @@ protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throw
121121
finally {
122122
monitor.stop();
123123
if (!this.trackAllInvocations || isLogEnabled(logger)) {
124-
logger.trace("JAMon performance statistics for method [" + name + "]:\n" + monitor);
124+
writeToLog(logger, "JAMon performance statistics for method [" + name + "]:\n" + monitor);
125125
}
126126
}
127127
}

spring-aop/src/main/java/org/springframework/aop/interceptor/PerformanceMonitorInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -63,7 +63,7 @@ protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throw
6363
}
6464
finally {
6565
stopWatch.stop();
66-
logger.trace(stopWatch.shortSummary());
66+
writeToLog(logger, stopWatch.shortSummary());
6767
}
6868
}
6969

spring-aop/src/main/java/org/springframework/aop/interceptor/SimpleTraceInterceptor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -55,14 +55,14 @@ public SimpleTraceInterceptor(boolean useDynamicLogger) {
5555
@Override
5656
protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable {
5757
String invocationDescription = getInvocationDescription(invocation);
58-
logger.trace("Entering " + invocationDescription);
58+
writeToLog(logger, "Entering " + invocationDescription);
5959
try {
6060
Object rval = invocation.proceed();
61-
logger.trace("Exiting " + invocationDescription);
61+
writeToLog(logger, "Exiting " + invocationDescription);
6262
return rval;
6363
}
6464
catch (Throwable ex) {
65-
logger.trace("Exception thrown in " + invocationDescription, ex);
65+
writeToLog(logger, "Exception thrown in " + invocationDescription, ex);
6666
throw ex;
6767
}
6868
}

0 commit comments

Comments
 (0)