Log4j按包过滤

Log4j按包过滤,log4j,Log4j,我想让log4j(v1.2.17)发送 。。。至文件附加器 来自所有软件包的信息(及以上) 。。。至控制台 如果来自“my.project”,则信息(及以上) 所有其他软件包的else WARN(及以上) 我该怎么做?如果可能的话,我更喜欢属性文件,但如果必要的话,我会切换到XML 我尝试了记录器、非可加性、阈值和LevelMatchFilter的组合,但没有找到答案。这肯定可以做到。下面,您将找到一些示例代码和log4j属性,它们将实现您想要的功能 下面是第一个示例类: package

我想让log4j(v1.2.17)发送

。。。至文件附加器

  • 来自所有软件包的信息(及以上)
。。。至控制台

  • 如果来自“my.project”,则信息(及以上)
  • 所有其他软件包的else WARN(及以上)
我该怎么做?如果可能的话,我更喜欢属性文件,但如果必要的话,我会切换到XML


我尝试了记录器、非可加性、阈值和LevelMatchFilter的组合,但没有找到答案。

这肯定可以做到。下面,您将找到一些示例代码和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
以下是运行示例1后紧接着示例2的控制台输出:

 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!