Log4j logger.throwing无法设置日志级别

时间:2017-06-27 19:40:44

标签: java logging

我对如何在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)

1 个答案:

答案 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的所有引用并使用较新的实现,或者硬声明完整的包名称。