如何配置log4j2 abstract appender以与TestNG一起使用

时间:2017-01-05 21:00:36

标签: java xml automated-tests testng log4j2

我已经阅读了有关使用log4j2和TestNG的所有文章,事实上,我正在使用code for ReporterAppender that I found on SO。不幸的是,我似乎无法让它发挥作用。我收到了两个非常模糊的错误,我也在这个网站上进行过研究,但是无济于事:

  1. 2017-01-05 11:49:29,743主要ERROR错误处理元素ReporterAppender([Appenders:null]):CLASS_NOT_FOUND
  2. 2017-01-05 11:49:29,866主要错误无法找到appender" Reporter" for logger config" root"
  3. 正如您可能从这些错误的格式中猜测的那样,我配置了多个appender。我的控制台appender和文件追加器工作得很漂亮。它只是TestNG Reporter的一个我无法工作的人。基于此以及跟踪输出中的信息,我确信它找到了我的log4j2.xml文件。我还尝试评论ReporterAppender的配置行,错误消失了。但是,就像原帖中的人一样,我想在我的log4j日志中输入TestNG报告信息。我怀疑问题出在log4j2.xml文件中。而且,更糟糕的是,我对Java相对较新。任何人都可以对此有所了解吗?我浪费了太多时间试图让它发挥作用并准备放弃。 Log4j2看起来非常强大,但学习曲线非常陡峭。任何帮助将不胜感激!提前谢谢。

    Log4j2.xml(已修改):

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="trace" packages="com.xyz.ReporterAppender">
      <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg%n"/>
        </Console>
        <File name="FileAppender" fileName="../logs/TestNG_${date:yyyyMMdd_HHMMSS}.log" append="false">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg%n"/>
        </File>
        <Reporter name="TestReporter">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg%n"/>
        </Reporter>
      </Appenders>
    
      <Loggers>
        <Root level="trace">
          <AppenderRef ref="Console"/>
          <AppenderRef ref="FileAppender"/>
          <AppenderRef ref="TestReporter"/>
        </Root>
      </Loggers>
    </Configuration>
    

    ReporterAppender.java:

    package com.xyz.ReporterAppender;
    
    import java.io.Serializable;
    
    import org.apache.logging.log4j.core.Layout;
    import org.apache.logging.log4j.core.LogEvent;
    import org.apache.logging.log4j.core.appender.AbstractAppender;
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    import org.apache.logging.log4j.core.config.plugins.PluginElement;
    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
    import org.apache.logging.log4j.core.layout.AbstractStringLayout;
    import org.testng.Reporter;
    
    @Plugin(name="Reporter", category ="Core", elementType="appender", printObject=true)
    public class ReporterAppender extends AbstractAppender {
    
        private ReporterAppender(final String name, final Layout layout) {
            super(name, null, layout, false);
        }
    
        @Override
        public void append(final LogEvent event) {
            final Layout<? extends Serializable> layout = getLayout();
            if (layout != null && layout instanceof AbstractStringLayout) {
                 Reporter.log(((AbstractStringLayout) layout).toSerializable(event));
            } else {
                 Reporter.log(event.getMessage().getFormattedMessage());
            }
        }
    
        @PluginFactory
        public static ReporterAppender createAppender(
            @PluginAttribute("name") @Required(message = "A name for the Appender must be specified") final String name,
            @PluginElement("Layout") Layout<? extends Serializable> layout) {
            return new ReporterAppender(name, layout);
        }
    }
    

    LoggingTest.java:

    package com.xyz.tests;
    
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.testng.annotations.Test;
    
    
    public class LoggingTest {
        private static final Logger logger = LogManager.getLogger();
    
      @Test()
      public void Test() {
          logger.info("Begin test.");
          logger.debug("Debug message");
          logger.error("Error message.");
          logger.trace("Trace message.");
      }
    }
    

0 个答案:

没有答案