Log4j按包过滤

时间:2015-12-12 19:50:41

标签: log4j

我想让log4j(v1.2.17)发送...

...到 fileAppender

  • 来自所有包的信息(及以上)

...到控制台

  • 如果来自“ my.project ”,那么INFO(及以上)
  • 其他所有其他包的WARN(及以上)

我该怎么做?如果可能的话,我更喜欢属性文件,但如果需要,我会切换到XML。

我尝试过记录器组合,非加性,阈值和& LevelMatchFilter但无法弄明白。

1 个答案:

答案 0 :(得分:1)

这绝对可以做到。您将在下面找到一些示例代码和log4j属性,它们可以满足您的需求。

这是第一个示例类:

package my;

import org.apache.log4j.Logger;

public class Example1 {
    private static final Logger logger = Logger.getLogger(Example1.class);

    public static void main(String[] args){
        logger.debug("debug from my.Example1 - should display nowhere!");
        logger.info("info from my.Example1 - should display in log file only!");
        logger.warn("warning from my.Example1 - should display in console and log file!");
        logger.error("error from my.Example1 - should display in console and log file!");
        logger.fatal("fatal from my.Example1 - should display in console and log file!");
    }
}

这是第二个示例类:

package my.project;

import org.apache.log4j.Logger;

public class Example2 {

    private static final Logger logger = Logger.getLogger(Example2.class);

    public static void main(String[] args){
        logger.debug("debug from my.project.Example2 - should display nowhere!");
        logger.info("info from my.project.Example2 - should display in console and log file!");
        logger.warn("warning from my.project.Example2 - should display in console and log file!");
        logger.error("error from my.project.Example2 - should display in console and log file!");
        logger.fatal("fatal from my.project.Example2 - should display in console and log file!");
    }
}

这是log4j.properties文件:

# The root logger will accept INFO messages or higher and send them to the log file
# and to a console appender that filters out any messages below WARN level
log4j.rootLogger=INFO, R, warnStdout

# Configure a console appender that will be used for messages of any level
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#This will be used to print WARN level or higher messages to console 
log4j.appender.warnStdout=org.apache.log4j.ConsoleAppender
log4j.appender.warnStdout.layout=org.apache.log4j.PatternLayout
log4j.appender.warnStdout.Threshold=WARN

# Pattern to output the caller's file name and line number.
log4j.appender.warnStdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=test.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

# Classes in the my.project package will accept messages of INFO level or higher
# and send those messages to the console and to the log file
log4j.logger.my.project=INFO, stdout, R
# Need to set additivity to false or else both the my.project and root loggers 
# will accept messages from classes in package my.project
log4j.additivity.my.project=false

这是运行Example1之后的控制台输出,紧接着是Example2:

 WARN [main] (Example1.java:11) - warning from my.Example1 - should display in console and log file!
ERROR [main] (Example1.java:12) - error from my.Example1 - should display in console and log file!
FATAL [main] (Example1.java:13) - fatal from my.Example1 - should display in console and log file!
 INFO [main] (Example2.java:11) - info from my.project.Example2 - should display in console and log file!
 WARN [main] (Example2.java:12) - warning from my.project.Example2 - should display in console and log file!
ERROR [main] (Example2.java:13) - error from my.project.Example2 - should display in console and log file!
FATAL [main] (Example2.java:14) - fatal from my.project.Example2 - should display in console and log file!

以下是运行Example1后跟Example2:

后的test.log文件内容
INFO main my.Example1 - info from my.Example1 - should display in log file only!
WARN main my.Example1 - warning from my.Example1 - should display in console and log file!
ERROR main my.Example1 - error from my.Example1 - should display in console and log file!
FATAL main my.Example1 - fatal from my.Example1 - should display in console and log file!
INFO main my.project.Example2 - info from my.project.Example2 - should display in console and log file!
WARN main my.project.Example2 - warning from my.project.Example2 - should display in console and log file!
ERROR main my.project.Example2 - error from my.project.Example2 - should display in console and log file!
FATAL main my.project.Example2 - fatal from my.project.Example2 - should display in console and log file!