Skip to content

Commit aa0d7a6

Browse files
committed
AbstractTraceInterceptor provides logExceptionStackTrace flag and writeToLog delegates
Issue: SPR-15763
1 parent e138d7d commit aa0d7a6

6 files changed

+70
-40
lines changed

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

Lines changed: 6 additions & 6 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.
@@ -22,12 +22,12 @@
2222

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

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

Lines changed: 56 additions & 3 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.
@@ -58,6 +58,12 @@ public abstract class AbstractTraceInterceptor implements MethodInterceptor, Ser
5858
*/
5959
private boolean hideProxyClassNames = false;
6060

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

6268
/**
6369
* Set whether to use a dynamic logger or a static logger.
@@ -98,6 +104,17 @@ public void setHideProxyClassNames(boolean hideProxyClassNames) {
98104
this.hideProxyClassNames = hideProxyClassNames;
99105
}
100106

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

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

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

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

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
@@ -275,29 +275,6 @@ protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throw
275275
}
276276
}
277277

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