0

如果我需要在包级别设置阈值,INFO我可以为我的包进行设置,MyPck例如:

log4j.logger.MyPck=INFO,debugLogFile

我可以不在包上设置阈值,而是在类级别上设置阈值?就像是 :

log4j.logger.MyPck.MyClass=INFO,debugLogFile
4

1 回答 1

0

简短的回答是Yes


但是,默认情况下,每个 logger 都继承其parent的属性。例如:

考虑简单的 Java 类:

package com.abc;

import org.apache.log4j.Logger;

class AnotherClass {
    private static Logger LOG = Logger.getLogger(AnotherClass.class);
    public AnotherClass() {
        LOG.trace("Instance created of AnotherClass");
    }
}

public class MyClass {
    private static Logger LOG = Logger.getLogger(MyClass.class);
    public MyClass() {
        LOG.trace("Instance created of MyClass");
    }
    public void foo() {
        LOG.info("Executing foo()...");
    }
    public static void main(String[] args) {
        new MyClass().foo();
        new AnotherClass();
    }
}

log4j.properties

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.SimpleLayout

log4j.logger.com.abc=TRACE,STDOUT
log4j.logger.com.abc.MyClass=INFO,STDOUT

在示例中,STDOUT被配置到 loggercom.abc中。因此,com.abc.MyClass继承并写入对象。查看输出:

INFO - Executing foo()...
INFO - Executing foo()...
TRACE - Instance created of AnotherClass

这是怎么回事?为什么要两次?该属性additivity可以防止这种传播。

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.SimpleLayout

log4j.logger.com.abc=TRACE,STDOUT
log4j.logger.com.abc.MyClass=INFO,STDOUT
log4j.additivity.com.abc.MyClass=false

所以,我们会看到:

INFO - Executing foo()...
TRACE - Instance created of AnotherClass
于 2013-10-30T04:24:33.460 回答