Skip to content

Commit

Permalink
refactor: rename ExceptionReporter to ExceptionLogger; and rename rel…
Browse files Browse the repository at this point in the history
…ated system property name
  • Loading branch information
oldratlee committed Dec 22, 2024
1 parent 4977274 commit 0ffaf2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import org.slf4j.spi.LocationAwareLogger;


/**
* <a href="https://peps.python.org/pep-0020/">Errors should never pass silently. Unless explicitly silenced.</a>
* Internal 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:
* <ul>
* <li>{@code full}: Log the complete exception stack trace (default)</li>
* <li>{@code short}: Log only the exception message</li>
* <li>{@code none}: Suppress all exception logging</li>
* </ul>
* <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>
* </ul>
*
* @author HuHao (995483610 at qq dot com)
* @author Jerry Lee (oldratlee at gmail dot com)
* @see <a href="https://peps.python.org/pep-0020/">Errors should never pass silently. Unless explicitly silenced.</a>
*/
final class ExceptionReporter {
private static final String FQCN = ExceptionReporter.class.getName();
final class ExceptionLogger {
private static final String FQCN = ExceptionLogger.class.getName();
private static final String CFFU_PACKAGE_NAME = FQCN.replaceFirst("\\.[^.]*$", "");

private static final LoggerAdapter logger = getLogger();

@SuppressWarnings("StatementWithEmptyBody")
static void reportUncaughtException(String where, Throwable ex) {
final String fullReport = "full";
final String shortReport = "short";
final String noneReport = "none";
static void logUncaughtException(String where, Throwable ex) {
final String fullFormat = "full";
final String shortFormat = "short";
final String noneFormat = "none";

final String report = System.getProperty("cffu.uncaught.exception.report", fullReport);
final String format = System.getProperty("cffu.uncaught.exception.log.format", fullFormat);
final String msgHead = "Uncaught exception occurred at ";
if (noneReport.equalsIgnoreCase(report)) {
if (noneFormat.equalsIgnoreCase(format)) {
// pass silently when explicitly silenced.
} else if (shortReport.equalsIgnoreCase(report)) {
} else if (shortFormat.equalsIgnoreCase(format)) {
logger.error(msgHead + where + ", " + ex, null);
} else {
logger.error(msgHead + where, ex);
Expand Down Expand Up @@ -67,6 +83,6 @@ public void error(String msg, @Nullable Throwable ex) {
}
}

private ExceptionReporter() {
private ExceptionLogger() {
}
}
6 changes: 3 additions & 3 deletions cffu-core/src/main/java/io/foldright/cffu/LLCF.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.function.BiConsumer;
import java.util.function.Supplier;

import static io.foldright.cffu.ExceptionReporter.reportUncaughtException;
import static io.foldright.cffu.ExceptionLogger.logUncaughtException;
import static io.foldright.cffu.InternalCommonUtils.mapArray;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
Expand Down Expand Up @@ -150,7 +150,7 @@ C peek0(C cfThis, BiConsumer<? super T, ? super Throwable> action, String where)
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException(where, e);
logUncaughtException(where, e);
}
});
return cfThis;
Expand All @@ -170,7 +170,7 @@ C peekAsync0(C cfThis, BiConsumer<? super T, ? super Throwable> action, String w
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException(where, e);
logUncaughtException(where, e);
}
}, executor);
return cfThis;
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.report=short
-Dcffu.uncaught.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.report=short
-Dcffu.uncaught.exception.log.format=short
)
# shellcheck disable=SC2034
readonly MVN_OPTS_FOR_JAVA17=(
-Dkotlin.version=1.8.0
-DswitchToLog4j2LoggingDependencies -Dcffu.uncaught.exception.report=short
-DswitchToLog4j2LoggingDependencies -Dcffu.uncaught.exception.log.format=short
)
# shellcheck disable=SC2034
readonly MVN_OPTS_FOR_JAVA23=(
-Dkotlin.version=1.9.0
-Dcffu.uncaught.exception.report=none
-Dcffu.uncaught.exception.log.format=none
)

SUREFIRE_TEST_GOAL=(surefire:test)
Expand Down

0 comments on commit 0ffaf2f

Please sign in to comment.