@@ -55,6 +55,8 @@ public final class JDK14LoggerAdapter extends LegacyAbstractLogger implements Lo
55
55
56
56
transient final java .util .logging .Logger logger ;
57
57
58
+ static int NOT_FOUND = -1 ;
59
+
58
60
// WARN: JDK14LoggerAdapter constructor should have only package access so
59
61
// that only JDK14LoggerFactory be able to create one.
60
62
JDK14LoggerAdapter (java .util .logging .Logger logger ) {
@@ -181,26 +183,10 @@ public void log(Marker marker, String callerFQCN, int slf4jLevelInt, String mess
181
183
final private void fillCallerData (String callerFQCN , LogRecord record ) {
182
184
StackTraceElement [] steArray = new Throwable ().getStackTrace ();
183
185
184
- int selfIndex = -1 ;
185
- for (int i = 0 ; i < steArray .length ; i ++) {
186
- final String className = steArray [i ].getClassName ();
187
-
188
- if (barrierMatch (callerFQCN , className )) {
189
- selfIndex = i ;
190
- break ;
191
- }
192
- }
193
-
194
- int found = -1 ;
195
- for (int i = selfIndex + 1 ; i < steArray .length ; i ++) {
196
- final String className = steArray [i ].getClassName ();
197
- if (!(barrierMatch (callerFQCN , className ))) {
198
- found = i ;
199
- break ;
200
- }
201
- }
186
+ int furthestIndex = findFurthestIndex (callerFQCN , steArray );
202
187
203
- if (found != -1 ) {
188
+ if (furthestIndex != NOT_FOUND ) {
189
+ int found = furthestIndex +1 ;
204
190
StackTraceElement ste = steArray [found ];
205
191
// setting the class name has the side effect of setting
206
192
// the needToInferCaller variable to false.
@@ -209,7 +195,24 @@ final private void fillCallerData(String callerFQCN, LogRecord record) {
209
195
}
210
196
}
211
197
212
- static String SELF = JDK14LoggerAdapter .class .getName ();
198
+ // find the furthest index which matches any of the barrier classes
199
+ // We assume that the actual caller is at most MAX_SEARCH_DEPTH calls away
200
+ private int findFurthestIndex (String callerFQCN , StackTraceElement [] steArray ) {
201
+
202
+ final int maxIndex = Math .min (MAX_SEARCH_DEPTH , steArray .length );
203
+ int furthestIndex = NOT_FOUND ;
204
+
205
+ for (int i = 0 ; i < maxIndex ; i ++) {
206
+ final String className = steArray [i ].getClassName ();
207
+ if (barrierMatch (callerFQCN , className )) {
208
+ furthestIndex = i ;
209
+ }
210
+ }
211
+ return furthestIndex ;
212
+ }
213
+
214
+ static final int MAX_SEARCH_DEPTH = 12 ;
215
+ static String SELF = JDK14LoggerAdapter .class .getName ();
213
216
214
217
static String SUPER = LegacyAbstractLogger .class .getName ();
215
218
static String SUPER_OF_SUPER = AbstractLogger .class .getName ();
0 commit comments