Commit 40abd138 authored by Phillip Webb's avatar Phillip Webb

Be defensive about hiding log errors

Ensure that log configuration errors always get reported.
parent 528fcf3d
...@@ -19,7 +19,10 @@ package org.springframework.boot; ...@@ -19,7 +19,10 @@ package org.springframework.boot;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* {@link UncaughtExceptionHandler} to suppress handling already logged exceptions. * {@link UncaughtExceptionHandler} to suppress handling already logged exceptions.
...@@ -28,6 +31,14 @@ import java.util.List; ...@@ -28,6 +31,14 @@ import java.util.List;
*/ */
class LoggedExceptionHandler implements UncaughtExceptionHandler { class LoggedExceptionHandler implements UncaughtExceptionHandler {
private static Set<String> LOG_CONFIGURATION_MESSAGES;
static {
Set<String> messages = new HashSet<String>();
messages.add("Logback configuration error detected");
LOG_CONFIGURATION_MESSAGES = Collections.unmodifiableSet(messages);
}
private static LoggedExceptionHandlerThreadLocal handler = new LoggedExceptionHandlerThreadLocal(); private static LoggedExceptionHandlerThreadLocal handler = new LoggedExceptionHandlerThreadLocal();
private final UncaughtExceptionHandler parent; private final UncaughtExceptionHandler parent;
...@@ -45,7 +56,7 @@ class LoggedExceptionHandler implements UncaughtExceptionHandler { ...@@ -45,7 +56,7 @@ class LoggedExceptionHandler implements UncaughtExceptionHandler {
@Override @Override
public void uncaughtException(Thread thread, Throwable ex) { public void uncaughtException(Thread thread, Throwable ex) {
try { try {
if (!isRegistered(ex) && this.parent != null) { if (isPassedToParent(ex) && this.parent != null) {
this.parent.uncaughtException(thread, ex); this.parent.uncaughtException(thread, ex);
} }
} }
...@@ -54,6 +65,26 @@ class LoggedExceptionHandler implements UncaughtExceptionHandler { ...@@ -54,6 +65,26 @@ class LoggedExceptionHandler implements UncaughtExceptionHandler {
} }
} }
private boolean isPassedToParent(Throwable ex) {
return isLogConfigurationMessage(ex) || !isRegistered(ex);
}
/**
* Check if the exception is a log configuration message, i.e. the log call might not
* have actually output anything.
*/
private boolean isLogConfigurationMessage(Throwable ex) {
String message = ex.getMessage();
if (message != null) {
for (String candidate : LOG_CONFIGURATION_MESSAGES) {
if (message.contains(candidate)) {
return true;
}
}
}
return false;
}
private boolean isRegistered(Throwable ex) { private boolean isRegistered(Throwable ex) {
if (this.exceptions.contains(ex)) { if (this.exceptions.contains(ex)) {
return true; return true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment