我是 log4j 的新手。正如我在互联网上读到的,子记录器继承父记录器设置。通常给出的示例是针对同一包中的两个类。但是如果这些类位于不同的包中怎么办?例如
import com.foo.Bar;
public class MyApp{
static Logger logger = Logger.getLogger(MyApp.class);
public static void main(String[] args) {
BasicConfigurator.configure(); // default logging level is debug
Bar bar = new Bar();
bar.doIt();
}
}
和不同包中的第二个类
package mypackage;
import org.apache.log4j.Logger;
public class Bar {
static Logger logger = Logger.getLogger(Bar.class);
public void doIt() {
logger.debug("Did it again!");
}
}
那么 Bar
类中的记录器级别是多少?
最佳答案
MyApp
位于默认包中。请注意,不建议使用默认包,如本question中已回答的那样。 .
我认为定义自定义<logger>
是不可能的对于默认包,所以它必须是 <root>
已应用。
除非您定义自定义 <logger>
对于 mypackage.Bar
, <root>
也适用于该类别。
除了它们都可能受 <root>
统治这一事实之外, MyApp
和Bar
与日志配置无关。
关于java - 不同包之间的 log4j 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345528/