Skip to content

Commit 3d9df95

Browse files
committed
refactor: improve log message of swallowed exceptions 🦢
1 parent de4fb35 commit 3d9df95

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

cffu-core/src/main/java/io/foldright/cffu/eh/ExHandleOfMultiplyCfsUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.concurrent.CompletionStage;
1010

1111
import static io.foldright.cffu.CompletableFutureUtils.unwrapCfException;
12+
import static io.foldright.cffu.internal.ExceptionLogger.logException;
1213
import static io.foldright.cffu.internal.ExceptionLogger.logUncaughtException;
1314

1415

@@ -79,7 +80,10 @@ public static ExceptionHandler cffuExHandler() {
7980
return CFFU_EX_HANDLER;
8081
}
8182

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

8488
/**
8589
* Creates new CompletionStages that only capture exception results from the input CompletionStages,

cffu-core/src/main/java/io/foldright/cffu/internal/ExceptionLogger.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* <strong>Internal</strong> exception logging utility for the cffu library.
1010
* <p>
1111
* By default, uncaught exceptions are logged with their complete stack traces. The logging behavior can be configured
12-
* through the system property {@code cffu.uncaught.exception.log.format} with the following values:
12+
* through the system property {@code cffu.exception.log.format} with the following values:
1313
* <ul>
1414
* <li>{@code full}: Log the complete exception stack trace (default)</li>
1515
* <li>{@code short}: Log only the exception message</li>
@@ -18,8 +18,8 @@
1818
* <p>
1919
* Configure the logging format by either:
2020
* <ul>
21-
* <li>Setting the JVM argument {@code -Dcffu.uncaught.exception.log.format=<value>} at startup</li>
22-
* <li>Calling {@code System.setProperty("cffu.uncaught.exception.log.format", "<value>")} programmatically</li>
21+
* <li>Setting the JVM argument {@code -Dcffu.exception.log.format=<value>} at startup</li>
22+
* <li>Calling {@code System.setProperty("cffu.exception.log.format", "<value>")} programmatically</li>
2323
* </ul>
2424
*
2525
* @author HuHao (995483610 at qq dot com)
@@ -33,22 +33,25 @@ public final class ExceptionLogger {
3333
private static final LoggerAdapter logger = getLogger();
3434

3535
@SuppressWarnings("StatementWithEmptyBody")
36-
public static void logUncaughtException(String where, Throwable ex) {
36+
public static void logException(String msg, Throwable ex) {
3737
final String fullFormat = "full";
3838
final String shortFormat = "short";
3939
final String noneFormat = "none";
4040

41-
final String format = System.getProperty("cffu.uncaught.exception.log.format", fullFormat);
42-
final String msgHead = "Uncaught exception occurred at ";
41+
final String format = System.getProperty("cffu.exception.log.format", fullFormat);
4342
if (noneFormat.equalsIgnoreCase(format)) {
4443
// pass silently when explicitly silenced.
4544
} else if (shortFormat.equalsIgnoreCase(format)) {
46-
logger.error(msgHead + where + ", " + ex, null);
45+
logger.error(msg + ", " + ex, null);
4746
} else {
48-
logger.error(msgHead + where, ex);
47+
logger.error(msg, ex);
4948
}
5049
}
5150

51+
public static void logUncaughtException(String where, Throwable ex) {
52+
logException("Uncaught exception occurred at " + where, ex);
53+
}
54+
5255
/**
5356
* Returns a logger adapter that uses {@code SLF4J} if available, otherwise uses {@link java.util.logging}.
5457
*/

scripts/integration_test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,23 @@ mvu::mvn_cmd clean install
6565
readonly MVN_OPTS_FOR_JAVA8=(
6666
-Dkotlin.version=1.6.0 -Dguava.version=29.0-jre
6767
-P!default-logging-dependencies -P!default-arch-unit-test
68-
-Dcffu.uncaught.exception.log.format=short
68+
-Dcffu.exception.log.format=short
6969
)
7070
# shellcheck disable=SC2034
7171
readonly MVN_OPTS_FOR_JAVA11=(
7272
-Dkotlin.version=1.7.0
7373
-DswitchToLog4j2LoggingDependencies -Pswitch-slf4j-to-v1
74-
-Dcffu.uncaught.exception.log.format=short
74+
-Dcffu.exception.log.format=short
7575
)
7676
# shellcheck disable=SC2034
7777
readonly MVN_OPTS_FOR_JAVA17=(
7878
-Dkotlin.version=1.8.0
79-
-DswitchToLog4j2LoggingDependencies -Dcffu.uncaught.exception.log.format=short
79+
-DswitchToLog4j2LoggingDependencies -Dcffu.exception.log.format=short
8080
)
8181
# shellcheck disable=SC2034
8282
readonly MVN_OPTS_FOR_JAVA23=(
8383
-Dkotlin.version=1.9.0
84-
-Dcffu.uncaught.exception.log.format=none
84+
-Dcffu.exception.log.format=none
8585
)
8686

8787
SUREFIRE_TEST_GOAL=(surefire:test)

0 commit comments

Comments
 (0)