我对如何在Apache Log4J中使用logger.throwing以使用特定日志级别记录异常感到困惑。
logger是org.apache.logging.log4j.Logger
的一个实例这很好用:
return logger.throwing(new Exception("Message"));
这不起作用。为什么呢?
return logger.throwing(Level.DEBUG, new Exception("Message"));
以下是编译器显示的错误:
[javac] return logger.throwing(Level.DEBUG, new Exception("Message"));
[javac] ^
[javac] method Logger.<T#1>throwing(org.apache.logging.log4j.Level,T#1) is not applicable
[javac] (cannot infer type-variable(s) T#1
[javac] (argument mismatch; org.apache.log4j.Level cannot be converted to org.apache.logging.log4j.Level))
[javac] method Logger.<T#2>throwing(T#2) is not applicable
[javac] (cannot infer type-variable(s) T#2
[javac] (actual and formal argument lists differ in length))
[javac] where T#1,T#2 are type-variables:
[javac] T#1 extends Throwable declared in method <T#1>throwing(org.apache.logging.log4j.Level,T#1)
[javac] T#2 extends Throwable declared in method <T#2>throwing(T#2)
答案 0 :(得分:0)
Log4j中有两种Level实现。一个是较老的,并且不会使用它,因为它来自较旧的log4j主要版本。
Older implementation of org.apache.log4j.Level from 2012, for Log4j 1.2
The newer implementation is org.apache.logging.log4j.Level from 2017, for Log4j 2.X
如果可能,删除对旧Level
的所有引用并使用较新的实现,或者硬声明完整的包名称。