-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.foldright.cffu; | ||
|
||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import io.foldright.cffu.logger.slf4j.Slf4jLogger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.spi.LocationAwareLogger; | ||
|
||
import java.util.logging.Level; | ||
|
||
|
||
class ExceptionReporter { | ||
private static final LoggerAdapter logger = getLogger(); | ||
|
||
@SuppressWarnings("DataFlowIssue") | ||
public static <T> T reportException(String msg, Throwable ex) { | ||
String report = System.getProperty("cffu.uncaught.exception.report", "short"); | ||
if ("full".equals(report)) { | ||
logger.error(msg, ex); | ||
} else if ("short".equals(report)) { | ||
logger.error(msg + ", cause: " + ex.getMessage(), null); | ||
} | ||
return null; | ||
} | ||
|
||
private static LoggerAdapter getLogger() { | ||
try { | ||
return new Slf4jLoggerAdapter(); | ||
} catch (NoClassDefFoundError e) { | ||
return new JulLoggerAdapter(); | ||
} | ||
} | ||
|
||
private static interface LoggerAdapter { | ||
void error(String msg, @Nullable Throwable thrown); | ||
|
||
void warn(String msg, @Nullable Throwable thrown); | ||
} | ||
|
||
private static class Slf4jLoggerAdapter implements LoggerAdapter { | ||
private static final String FQCN = Slf4jLogger.class.getName(); | ||
|
||
private final org.slf4j.Logger logger = LoggerFactory.getLogger("cffu"); | ||
|
||
@Override | ||
public void error(String msg, @Nullable Throwable thrown) { | ||
if (logger instanceof LocationAwareLogger) { | ||
((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, thrown); | ||
} else { | ||
if (thrown != null) { | ||
logger.error(msg, thrown); | ||
} else { | ||
logger.error(msg); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void warn(String msg, @Nullable Throwable thrown) { | ||
if (logger instanceof LocationAwareLogger) { | ||
((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, thrown); | ||
} else { | ||
if (thrown != null) { | ||
logger.error(msg, thrown); | ||
} else { | ||
logger.error(msg); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static class JulLoggerAdapter implements LoggerAdapter { | ||
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger("cffu"); | ||
|
||
@Override | ||
public void error(String msg, @Nullable Throwable thrown) { | ||
logger.log(Level.SEVERE, msg, thrown); | ||
} | ||
|
||
@Override | ||
public void warn(String msg, @Nullable Throwable thrown) { | ||
logger.log(Level.WARNING, msg, thrown); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.foldright.cffu; | ||
|
||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
|
||
public class LDemo { | ||
public static void main(String[] args) { | ||
if(ThreadLocalRandom.current().nextBoolean()) { | ||
System.setProperty("cffu.uncaught.exception.report", "full"); | ||
System.out.println("set full to cffu.uncaught.exception.report"); | ||
} | ||
ExceptionReporter.reportException("hello", new RuntimeException("Bang!")); | ||
} | ||
} |