1919import java .lang .Thread .UncaughtExceptionHandler ;
2020import java .lang .reflect .InvocationTargetException ;
2121import java .util .ArrayList ;
22+ import java .util .Collections ;
23+ import java .util .HashSet ;
2224import java .util .List ;
25+ import java .util .Set ;
2326
2427/**
2528 * {@link UncaughtExceptionHandler} to suppress handling already logged exceptions.
2831 */
2932class 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