Skip to content

Commit

Permalink
log 和 异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
huhaosumail committed Jul 1, 2024
1 parent e614c90 commit 3839375
Show file tree
Hide file tree
Showing 16 changed files with 1,659 additions and 15 deletions.
24 changes: 24 additions & 0 deletions cffu-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<optional>true</optional>
</dependency>

</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.Nullable;
import io.foldright.cffu.logger.ConfigReportException;
import io.foldright.cffu.tuple.Tuple2;
import io.foldright.cffu.tuple.Tuple3;
import io.foldright.cffu.tuple.Tuple4;
Expand Down Expand Up @@ -1606,12 +1607,33 @@ public static Executor defaultExecutor() {
return AsyncPoolHolder.ASYNC_POOL;
}

// endregion

////////////////////////////////////////////////////////////
// region## unwrapCfException(static methods)
////////////////////////////////////////////////////////////
/**
* A convenient util method for converting input package {@link Throwable} to root {@link Throwable}.
* @param throwable
*/
public static Throwable unwrapCfException(Throwable throwable) {
if (throwable instanceof CompletionException || throwable instanceof ExecutionException) {
if (throwable.getCause() != null) {
return throwable.getCause();
}
}
return throwable;
}

// endregion
// endregion

////////////////////////////////////////////////////////////////////////////////
// region# CF Instance Methods(including new enhanced + backport methods)
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////
// region## Then-Multi-Actions(thenM*) Methods
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3020,7 +3042,7 @@ else delayedExecutor(0, TimeUnit.SECONDS, asyncExecutor)
// use `cf.handle` method(instead of `whenComplete`) and return null,
// in order to prevent below `exceptionally` reporting the handled argument exception in this action
return null;
}).exceptionally(ex -> reportException("Exception occurred in the input cf whenComplete of hop executor:", ex));
}).exceptionally(ex -> ConfigReportException.reportException("Exception occurred in the input cf whenComplete of hop executor:", ex));

return (C) ret;
}
Expand All @@ -3031,23 +3053,12 @@ private static void completeCf(CompletableFuture<Object> cf, Object value, @Null
else cf.completeExceptionally(ex);
} catch (Throwable t) {
if (ex != null) t.addSuppressed(ex);
reportException("Exception occurred in completeCf:", t);
ConfigReportException.reportException("Exception occurred in completeCf:", t);
throw t; // rethrow exception, report to caller
}
}

@Nullable
@SuppressWarnings("SameReturnValue")
private static <T> T reportException(String msg, Throwable ex) {
StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);

writer.println(msg);
ex.printStackTrace(writer);

System.err.println(sw);
return null;
}

// endregion
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3137,7 +3148,7 @@ C peek(C cf, BiConsumer<? super T, ? super Throwable> action) {
requireNonNull(cf, "cf is null");
requireNonNull(action, "action is null");

cf.whenComplete(action).exceptionally(ex -> reportException("Exception occurred in the action of peek:", ex));
cf.whenComplete(action).exceptionally(ex -> ConfigReportException.reportException("Exception occurred in the action of peek:", ex));
return cf;
}

Expand Down Expand Up @@ -3188,7 +3199,7 @@ C peekAsync(C cf, BiConsumer<? super T, ? super Throwable> action, Executor exec
requireNonNull(executor, "executor is null");

cf.whenCompleteAsync(action, executor).exceptionally(ex ->
reportException("Exception occurred in the action of peekAsync:", ex));
ConfigReportException.reportException("Exception occurred in the action of peekAsync:", ex));
return cf;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.foldright.cffu.logger;

import edu.umd.cs.findbugs.annotations.Nullable;
import io.foldright.cffu.logger.jdk.JdkLoggerAdapter;
import io.foldright.cffu.logger.log4j.Log4jLoggerAdapter;
import io.foldright.cffu.logger.log4j2.Log4j2LoggerAdapter;
import io.foldright.cffu.logger.slf4j.Slf4jLoggerAdapter;


public class ConfigReportException {

private static Logger logger;

/**
* cffu.uncaught.exception.report=none | oneline | short(default) | full
* corresponding system print|error|warn|info
* @param msg
* @param ex
* @param <T>
* @return
*/
@Nullable
@SuppressWarnings("SameReturnValue")
public static <T> T reportException(String msg, Throwable ex) {

String loggerLevel = System.getProperty("cffu.uncaught.exception.report.log.level", "none");
switch (loggerLevel){
case "warn":
logger.warn(msg, ex);
break;
case "info":
logger.info(msg, ex);
break;
case "error":
logger.error(msg, ex);
break;
case "none":
default:
//no handle
break;
}
return null;
}

static {
String appLogger = System.getProperty("cffu.uncaught.exception.report.log.type", "none");
LoggerAdapter loggerAdapter;
switch (appLogger){
case "log4j":
loggerAdapter = new Log4jLoggerAdapter();
break;
case "log4j2":
loggerAdapter = new Log4j2LoggerAdapter();
break;
case "slf4j":
loggerAdapter = new Slf4jLoggerAdapter();
break;
case "none":
default:
loggerAdapter = new JdkLoggerAdapter();
break;
}
logger = loggerAdapter.getLogger(ConfigReportException.class.getName());

String loggerLevel = System.getProperty("cffu.uncaught.exception.report.log.level", "none");
switch (loggerLevel){
case "warn":
loggerAdapter.setLevel(Level.WARN);
break;
case "info":
loggerAdapter.setLevel(Level.INFO);
break;
case "error":
loggerAdapter.setLevel(Level.ERROR);
break;
case "none":
default:
loggerAdapter.setLevel(Level.OFF);
//no handle
break;
}

}

}
43 changes: 43 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/logger/Level.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

package io.foldright.cffu.logger;

/**
* Level
*/
public enum Level {

/**
* ALL
*/
ALL,

/**
* TRACE
*/
TRACE,

/**
* DEBUG
*/
DEBUG,

/**
* INFO
*/
INFO,

/**
* WARN
*/
WARN,

/**
* ERROR
*/
ERROR,

/**
* OFF
*/
OFF
}
Loading

0 comments on commit 3839375

Please sign in to comment.