0

我正在使用由如下所示的几个日志方法组成的侦听器类来记录 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
4

1 回答 1

0
  • 从根记录器中删除控制台附加程序

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

    log4j.logger.xxxx.SimpleListener =INFO, ConsoleAppender
    

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

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

    log4j.additivity.xxxx.SimpleListener=false
    
于 2013-03-27T07:30:03.540 回答