指定特定类的级别

时间:2013-10-29 13:55:03

标签: java log4j

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

log4j.logger.MyPck=INFO,debugLogFile

我可以在包装上制作门槛,但是在课堂上吗?类似的东西:

log4j.logger.MyPck.MyClass=INFO,debugLogFile

1 个答案:

答案 0 :(得分:0)

简短的回答是


但是,默认情况下,每个记录器都会继承其父级的属性。 e.g:

考虑简单的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已配置到记录器com.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