在tinylog上做实验时,我面临的问题是无法打印运行时异常日志。
对于ex。int a =0 ;int b= 10;int c= b/a;
因为这将给出算术异常,但这不是在日志中获取记录器。
发布于 2021-09-24 13:11:48
您只需要捕获运行时异常并将其传递给记录器:
import org.tinylog.Logger;
public class Application {
public static void main(String[] args) {
try {
int a = 0, b = 10;
System.out.println(b / a);
} catch (RuntimeException ex) {
Logger.error(ex);
}
}
}编辑:或者,您也可以使用UncaughtExceptionHandler来记录未捕获的异常:
import java.lang.Thread.UncaughtHandler;
import org.tinylog.Logger;
public class Application {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new TinylogHandler());
int a = 0, b = 0;
System.out.println(a / b);
}
private static class TinylogHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Logger.error(ex);
}
}
}https://stackoverflow.com/questions/69298753
复制相似问题