java Log4J:isDebugEnabled() 方法有问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4J: problem with isDebugEnabled() method
提问by julien
I am struggling with Log4J and isDebugEnabled() method.
我正在努力使用 Log4J 和 isDebugEnabled() 方法。
When I execute:
当我执行:
package org.test;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Test {
public static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String[] args) { (new Test()).test(); }
public void test() {
System.out.println("Logger " + logger.getName());
System.out.println("level: " + logger.getLevel());
logger.setLevel(Level.DEBUG);
System.out.println("level: " + logger.getLevel());
System.out.println("debug? " + logger.isDebugEnabled());
}
}
I get:
我得到:
Logger org.test.Test
level: null
level: DEBUG
debug? false
The logger level is obviously DEBUG
, but logger.isDebugEnabled()
returns false.
Do you have an idea how to fix that?
记录器级别显然是DEBUG
,但logger.isDebugEnabled()
返回 false。你知道如何解决这个问题吗?
EDIT:
I have tried with other versions of log4j and with a Level
cast, and it did not change anything.
编辑:我尝试过其他版本的 log4j 和Level
演员表,但它没有改变任何东西。
采纳答案by julien
The reason seems to be a conflict with a dependency introduced through maven. I have tested to delete some of these dependencies, and it finally worked. I do not understand why, but... it works!
原因似乎是与通过maven引入的依赖冲突。我已经测试删除了其中的一些依赖项,它终于奏效了。我不明白为什么,但是......它有效!
回答by drekka
I'd suggest you stop doing static Logger ....
. there is a very good article about the pros and conswhich is well worth a read, epecially if you are on a server.
我建议你停止做static Logger ....
。有一篇关于优缺点的非常好的文章值得一读,特别是如果您在服务器上。
Another suggest I'd make would also be to look at Slf4jor LogBackif you can consider alternatives. Slf4j for example, doesn't require guard "if" statements around debug logging which in turn means cleaner code.
如果您可以考虑替代方案,我还会提出的另一个建议是查看Slf4j或LogBack。例如,Slf4j 不需要围绕调试日志的保护“if”语句,这反过来意味着更干净的代码。
回答by Andreas Dolk
I tried to reproduce it with this code, but it works for me:
我试图用这段代码重现它,但它对我有用:
package com.example;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LogTest {
public static Logger logger = Logger.getLogger(LogTest.class.getName());
public static void main(String[] args) {
System.out.println(logger.getLevel());
logger.setLevel(Level.DEBUG);
System.out.println(logger.getLevel());
System.out.println(logger.isDebugEnabled());
}
}
Output:
输出:
null
DEBUG
true
I've used log4j-1.2.15 for this test.
我在这个测试中使用了 log4j-1.2.15。
So maybe there's an issue with your version of log4j - switch to 1.2.14 or 1.2.15 for a moment and just check, if the problem is still there.
因此,您的 log4j 版本可能存在问题 - 暂时切换到 1.2.14 或 1.2.15,然后检查问题是否仍然存在。
回答by Nico Huysamen
You should do the following (from JavaDoc):
您应该执行以下操作(来自 JavaDoc):
logger.setLevel((Level) Level.DEBUG);
--
——
The only other difference I see between our examples is you have:
我在我们的示例之间看到的唯一其他区别是:
public static Logger logger = Logger.getLogger(Test.class.getName());
where I have:
我有:
public static Logger logger = Logger.getLogger(Test.class);
It's 99% probably not that, but as I said, that's the only difference.
99% 可能不是那样,但正如我所说,这是唯一的区别。