Skip to content

Commit 40abd13

Browse files
committed
Be defensive about hiding log errors
Ensure that log configuration errors always get reported.
1 parent 528fcf3 commit 40abd13

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/LoggedExceptionHandler.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.lang.Thread.UncaughtExceptionHandler;
2020
import java.lang.reflect.InvocationTargetException;
2121
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.HashSet;
2224
import java.util.List;
25+
import java.util.Set;
2326

2427
/**
2528
* {@link UncaughtExceptionHandler} to suppress handling already logged exceptions.
@@ -28,6 +31,14 @@
2831
*/
2932
class LoggedExceptionHandler implements UncaughtExceptionHandler {
3033

34+
private static Set<String> LOG_CONFIGURATION_MESSAGES;
35+
36+
static {
37+
Set<String> messages = new HashSet<String>();
38+
messages.add("Logback configuration error detected");
39+
LOG_CONFIGURATION_MESSAGES = Collections.unmodifiableSet(messages);
40+
}
41+
3142
private static LoggedExceptionHandlerThreadLocal handler = new LoggedExceptionHandlerThreadLocal();
3243

3344
private final UncaughtExceptionHandler parent;
@@ -45,7 +56,7 @@ public void register(Throwable exception) {
4556
@Override
4657
public void uncaughtException(Thread thread, Throwable ex) {
4758
try {
48-
if (!isRegistered(ex) && this.parent != null) {
59+
if (isPassedToParent(ex) && this.parent != null) {
4960
this.parent.uncaughtException(thread, ex);
5061
}
5162
}
@@ -54,6 +65,26 @@ public void uncaughtException(Thread thread, Throwable ex) {
5465
}
5566
}
5667

68+
private boolean isPassedToParent(Throwable ex) {
69+
return isLogConfigurationMessage(ex) || !isRegistered(ex);
70+
}
71+
72+
/**
73+
* Check if the exception is a log configuration message, i.e. the log call might not
74+
* have actually output anything.
75+
*/
76+
private boolean isLogConfigurationMessage(Throwable ex) {
77+
String message = ex.getMessage();
78+
if (message != null) {
79+
for (String candidate : LOG_CONFIGURATION_MESSAGES) {
80+
if (message.contains(candidate)) {
81+
return true;
82+
}
83+
}
84+
}
85+
return false;
86+
}
87+
5788
private boolean isRegistered(Throwable ex) {
5889
if (this.exceptions.contains(ex)) {
5990
return true;

0 commit comments

Comments
 (0)