2015-12-12 53 views
0

我想有log4j的(v1.2.17)發送...Log4j的過濾器由包

...到fileAppender

  • INFO(及以上)

...到控制檯

  • 如果從 「my.project」 然後INFO(及以上)
  • 其他WARN(及以上)的所有其他包

我怎樣才能做到這一點?如果可能的話,我更喜歡一個屬性文件,但如果需要的話會切換到XML。

我試過記錄器,非疊加,閾值,& LevelMatchFilter的組合,但無法弄清楚。

回答

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 

這裏是控制檯在運行後輸出示例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! 

這裏是運行示例1後面例題之後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!