如何配置log4j属性文件以仅在控制台中打印监听器类日志?

时间:2013-03-27 06:08:33

标签: log4j testng

我正在使用Listener Class,它由几个日志方法组成,如下所示,以记录TestNG测试。我希望只在我的控制台中打印监听器类日志,并且完整日志会转到FullLog文件。完整日志正在转移到完整日志文件,但在控制台中我将获得完整日志和监听器类日志。如何配置log4j属性文件。

听众课程:

public class SimpleListener implements ITestListener {           
    private static Logger m_log = Logger.getLogger(SimpleListener.class);  

    public void onFinish(ITestContext itestcontext) {          
        m_log.info("onFinish() is called");      }        

    public void onStart(ITestContext itestcontext) {         
        m_log.info("onStart() is called");      
        }        
}

我的log4j属性文件:

log4j.rootLogger=INFO, FullLogAppender, ConsoleAppender
log4j.logger.TestLog=INFO, BreakPointAppender

#Console Log Appender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.ConsoleAppender.Target=System.out

#Full Log Appender
log4j.appender.FullLogAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FullLogAppender.File=mfgLogs/FullLogs/FullLog.log
log4j.appender.FullLogAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FullLogAppender.layout.ConversionPattern=%d %-5p %c  %x- %m%n
log4j.appender.FullLogAppender.Append=false Dec 12 12:41:11 IST 2012

控制台中的预期结果:

11:29:33,159  INFO SimpleListener:15 - onStart() is called
11:29:50,372  INFO SimpleListener:31 - runTest.....
11:30:23,637  INFO SimpleListener:23 - Failed 
11:30:24,402  INFO SimpleListener:12 - onFinish() is called

1 个答案:

答案 0 :(得分:0)

  • 从root logger中删除控制台appender

     log4j.rootLogger=INFO, FullLogAppender
    
  • 为您的听众添加记录器

    log4j.logger.xxxx.SimpleListener =INFO, ConsoleAppender
    

xxxx.SimpleListener将是您的侦听器的完全限定类名;

  • 如果您不希望侦听器的日志出现在完整日志文件中,请将additivity设置为false

    log4j.additivity.xxxx.SimpleListener=false