我必须是愚蠢的东西,但我似乎无法使用SLF4J的varargs-utilizing参数化日志记录方法。 一个例子:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggingTest {
@Test
public void loggingTest() {
Logger logger = LoggerFactory.getLogger(this.getClass());
int x = 0xdeadbeef;
long y = 0xdeadbeef;
try {
throw new Exception("This is a mighty exception!");
} catch(Exception e) {
logger.error("I wanna log {} and {} and {} with backtrace", x, y, 3, e);
}
}
}
在日志记录方法中,eclipse会产生这样的警告:
The method error(String, Object, Object) in the type Logger is not applicable for the arguments (String, int, long, int, Exception)
并且无法编译。
但是,如果我将日志记录调用更改为:
logger.error("I wanna log {} and {} and {} with backtrace", new Object[]{x, y, 3, e});
它按预期编译并运行(记录3个“变量”和异常堆栈跟踪)。
库版本是:slf4j-api-1.7.5.jar,slf4j-log4j12-1.7.5.jar和log4j-1.2.14.jar,如果它有任何区别。
如果有人会指出我的思维能力的缺点,那就非常感激了!
答案 0 :(得分:24)
我做了一些额外的调查,是获得
编译错误的唯一方法logger.error("I wanna log {} and {} and {} with backtrace", x, y, 3, e);
而不是
logger.error("I wanna log {} and {} and {} with backtrace", new Object[]{x, y, 3, e});
是使用1.7之前的SLF4J API版本(其中引入了对varargs的支持)。您可能需要深入了解类路径(或服务器运行时?)以查找以下语句无效的位置:
库版本是:slf4j-api-1.7.5.jar,slf4j-log4j12-1.7.5.jar和log4j-1.2.14.jar,如果它有任何区别。
(因为它确实恰好使您观察到的差异)