Skip to content

Commit 69c333d

Browse files
committed
fix issues/425
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 2470fad commit 69c333d

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

slf4j-jdk14/src/main/java/org/slf4j/jul/JDK14LoggerAdapter.java

+23-20
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public final class JDK14LoggerAdapter extends LegacyAbstractLogger implements Lo
5555

5656
transient final java.util.logging.Logger logger;
5757

58+
static int NOT_FOUND = -1;
59+
5860
// WARN: JDK14LoggerAdapter constructor should have only package access so
5961
// that only JDK14LoggerFactory be able to create one.
6062
JDK14LoggerAdapter(java.util.logging.Logger logger) {
@@ -181,26 +183,10 @@ public void log(Marker marker, String callerFQCN, int slf4jLevelInt, String mess
181183
final private void fillCallerData(String callerFQCN, LogRecord record) {
182184
StackTraceElement[] steArray = new Throwable().getStackTrace();
183185

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);
202187

203-
if (found != -1) {
188+
if (furthestIndex != NOT_FOUND) {
189+
int found = furthestIndex+1;
204190
StackTraceElement ste = steArray[found];
205191
// setting the class name has the side effect of setting
206192
// the needToInferCaller variable to false.
@@ -209,7 +195,24 @@ final private void fillCallerData(String callerFQCN, LogRecord record) {
209195
}
210196
}
211197

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();
213216

214217
static String SUPER = LegacyAbstractLogger.class.getName();
215218
static String SUPER_OF_SUPER = AbstractLogger.class.getName();

0 commit comments

Comments
 (0)