Skip to content

Commit

Permalink
refactor: improve log message of swallowed exceptions 🦢
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jan 1, 2025
1 parent de4fb35 commit 3d9df95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.CompletionStage;

import static io.foldright.cffu.CompletableFutureUtils.unwrapCfException;
import static io.foldright.cffu.internal.ExceptionLogger.logException;
import static io.foldright.cffu.internal.ExceptionLogger.logUncaughtException;


Expand Down Expand Up @@ -79,7 +80,10 @@ public static ExceptionHandler cffuExHandler() {
return CFFU_EX_HANDLER;
}

private static final ExceptionHandler CFFU_EX_HANDLER = exInfo -> logUncaughtException(exInfo.where, exInfo.ex);
private static final ExceptionHandler CFFU_EX_HANDLER = exInfo -> {
String msg = "Swallowed exception of cf" + (exInfo.index + 1) + " at " + exInfo.where;
logException(msg, exInfo.ex);
};

/**
* Creates new CompletionStages that only capture exception results from the input CompletionStages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* <strong>Internal</strong> exception logging utility for the cffu library.
* <p>
* By default, uncaught exceptions are logged with their complete stack traces. The logging behavior can be configured
* through the system property {@code cffu.uncaught.exception.log.format} with the following values:
* through the system property {@code cffu.exception.log.format} with the following values:
* <ul>
* <li>{@code full}: Log the complete exception stack trace (default)</li>
* <li>{@code short}: Log only the exception message</li>
Expand All @@ -18,8 +18,8 @@
* <p>
* Configure the logging format by either:
* <ul>
* <li>Setting the JVM argument {@code -Dcffu.uncaught.exception.log.format=<value>} at startup</li>
* <li>Calling {@code System.setProperty("cffu.uncaught.exception.log.format", "<value>")} programmatically</li>
* <li>Setting the JVM argument {@code -Dcffu.exception.log.format=<value>} at startup</li>
* <li>Calling {@code System.setProperty("cffu.exception.log.format", "<value>")} programmatically</li>
* </ul>
*
* @author HuHao (995483610 at qq dot com)
Expand All @@ -33,22 +33,25 @@ public final class ExceptionLogger {
private static final LoggerAdapter logger = getLogger();

@SuppressWarnings("StatementWithEmptyBody")
public static void logUncaughtException(String where, Throwable ex) {
public static void logException(String msg, Throwable ex) {
final String fullFormat = "full";
final String shortFormat = "short";
final String noneFormat = "none";

final String format = System.getProperty("cffu.uncaught.exception.log.format", fullFormat);
final String msgHead = "Uncaught exception occurred at ";
final String format = System.getProperty("cffu.exception.log.format", fullFormat);
if (noneFormat.equalsIgnoreCase(format)) {
// pass silently when explicitly silenced.
} else if (shortFormat.equalsIgnoreCase(format)) {
logger.error(msgHead + where + ", " + ex, null);
logger.error(msg + ", " + ex, null);
} else {
logger.error(msgHead + where, ex);
logger.error(msg, ex);
}
}

public static void logUncaughtException(String where, Throwable ex) {
logException("Uncaught exception occurred at " + where, ex);
}

/**
* Returns a logger adapter that uses {@code SLF4J} if available, otherwise uses {@link java.util.logging}.
*/
Expand Down
8 changes: 4 additions & 4 deletions scripts/integration_test
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ mvu::mvn_cmd clean install
readonly MVN_OPTS_FOR_JAVA8=(
-Dkotlin.version=1.6.0 -Dguava.version=29.0-jre
-P!default-logging-dependencies -P!default-arch-unit-test
-Dcffu.uncaught.exception.log.format=short
-Dcffu.exception.log.format=short
)
# shellcheck disable=SC2034
readonly MVN_OPTS_FOR_JAVA11=(
-Dkotlin.version=1.7.0
-DswitchToLog4j2LoggingDependencies -Pswitch-slf4j-to-v1
-Dcffu.uncaught.exception.log.format=short
-Dcffu.exception.log.format=short
)
# shellcheck disable=SC2034
readonly MVN_OPTS_FOR_JAVA17=(
-Dkotlin.version=1.8.0
-DswitchToLog4j2LoggingDependencies -Dcffu.uncaught.exception.log.format=short
-DswitchToLog4j2LoggingDependencies -Dcffu.exception.log.format=short
)
# shellcheck disable=SC2034
readonly MVN_OPTS_FOR_JAVA23=(
-Dkotlin.version=1.9.0
-Dcffu.uncaught.exception.log.format=none
-Dcffu.exception.log.format=none
)

SUREFIRE_TEST_GOAL=(surefire:test)
Expand Down

0 comments on commit 3d9df95

Please sign in to comment.