Skip to content

Commit

Permalink
By default install uncaught exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jhannes committed Sep 2, 2023
1 parent 556dab2 commit 994a617
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion logevents/src/main/java/org/logevents/LogEventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void setObservers(Map<String, Supplier<? extends LogEventObserver>> obser
* Detects whether we are currently running in a unit test. Used to set default log level.
*/
public boolean isRunningInsideJunit() {
for (StackTraceElement stackTraceElement : new Throwable().getStackTrace()) {
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
if (stackTraceElement.getClassName().matches("^org.junit.(runners|platform.engine).*")) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public boolean getBoolean(String key) {
return getBoolean(key, false);
}

public boolean getBoolean(String key, boolean other) {
return optionalString(key).map(Boolean::valueOf).orElse(other);
public boolean getBoolean(String key, boolean defaultValue) {
return optionalString(key).map(Boolean::valueOf).orElse(defaultValue);
}

public URL getUrl(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ protected Map<String, Supplier<? extends LogEventObserver>> createObservers(Map<
}

private void installUncaughtExceptionHandler(LogEventFactory factory, Configuration logeventsConfig) {
if (logeventsConfig.getBoolean("installExceptionHandler")) {
if (getShouldInstallExceptionHandler(logeventsConfig)) {
if (Thread.getDefaultUncaughtExceptionHandler() == null) {
Thread.setDefaultUncaughtExceptionHandler((thread, e) ->
factory.getRootLogger().error("Thread {} terminated with unhandled exception", thread.getName(), e));
Expand All @@ -373,6 +373,10 @@ private void installUncaughtExceptionHandler(LogEventFactory factory, Configurat
}
}

protected boolean getShouldInstallExceptionHandler(Configuration logeventsConfig) {
return logeventsConfig.getBoolean("installExceptionHandler", true);
}

protected void createObservers(Map<String, Supplier<? extends LogEventObserver>> observers, Map<String, String> configuration, Map<String, String> environment) {
for (Object key : configuration.keySet()) {
if (key.toString().matches("observer\\.\\w+")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ protected Level getDefaultRootLevel() {
return Level.WARN;
}

@Override
protected boolean getShouldInstallExceptionHandler(Configuration logeventsConfig) {
return logeventsConfig.getBoolean("installExceptionHandler", false);
}

public static String getTestMethodName(StackTraceElement[] stackTrace) {
return getMethodRef(getTestMethod(stackTrace));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ public void shouldLogNullWhenTestThreadIsNotFound() throws InterruptedException

@Test
public void shouldInstallDefaultExceptionHandler() throws InterruptedException {
Thread.setDefaultUncaughtExceptionHandler(null);
configuration.put("logevents.installExceptionHandler", "true");
configurator.applyConfigurationProperties(factory, configuration);
CircularBufferLogEventObserver rootObserver = new CircularBufferLogEventObserver();
Expand Down

0 comments on commit 994a617

Please sign in to comment.