1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -58,6 +58,12 @@ public abstract class AbstractTraceInterceptor implements MethodInterceptor, Ser
58
58
*/
59
59
private boolean hideProxyClassNames = false ;
60
60
61
+ /**
62
+ * Indicates whether to pass an exception to the logger.
63
+ * @see #writeToLog(Log, String, Throwable)
64
+ */
65
+ private boolean logExceptionStackTrace = true ;
66
+
61
67
62
68
/**
63
69
* Set whether to use a dynamic logger or a static logger.
@@ -98,6 +104,17 @@ public void setHideProxyClassNames(boolean hideProxyClassNames) {
98
104
this .hideProxyClassNames = hideProxyClassNames ;
99
105
}
100
106
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
+
101
118
102
119
/**
103
120
* Determines whether or not logging is enabled for the particular {@code MethodInvocation}.
@@ -171,6 +188,40 @@ protected boolean isLogEnabled(Log logger) {
171
188
return logger .isTraceEnabled ();
172
189
}
173
190
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
+
174
225
175
226
/**
176
227
* Subclasses must override this method to perform any tracing around the
@@ -180,13 +231,15 @@ protected boolean isLogEnabled(Log logger) {
180
231
* <p>By default, the passed-in {@code Log} instance will have log level
181
232
* "trace" enabled. Subclasses do not have to check for this again, unless
182
233
* 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.
184
236
* @param logger the {@code Log} to write trace messages to
185
237
* @return the result of the call to {@code MethodInvocation.proceed()}
186
238
* @throws Throwable if the call to {@code MethodInvocation.proceed()}
187
239
* encountered any errors
188
- * @see #isInterceptorEnabled
189
240
* @see #isLogEnabled
241
+ * @see #writeToLog(Log, String)
242
+ * @see #writeToLog(Log, String, Throwable)
190
243
*/
191
244
protected abstract Object invokeUnderTrace (MethodInvocation invocation , Log logger ) throws Throwable ;
192
245
0 commit comments