我对如何使用 logger.throwing in Apache Log4J 使用特定日志级别记录异常有些困惑。
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)
Log4j 中有两种 Level 实现。一个较旧,不能使用它,因为它来自较旧的 log4j 主要版本。
2012 年 org.apache.log4j.Level 的较旧实现,适用于 Log4j 1.2
较新的实现是 2017 年的 org.apache.logging.log4j.level,适用于 Log4j 2.X
如果可能,请删除对旧Level
的所有引用并使用较新的实现,或硬声明完整的包名称。