Is there a log4j appender that connects with TestNG?

流过昼夜 提交于 2019-12-05 00:24:32

问题


I use log4j and would like log messages that normally end up in my logging facility to appear in the test reports created by TestNG during my unit tests.

I think that would mean a log4j Appender which outputs to a TestNG Listener and an appropriate log4j config in the src/test/resources directory of my Maven project. Is that correct?

It seems fairly easy to write, but is there something I just can pull in via Maven?


回答1:


I had the same problem and eventually coded an appender myself. It is actually quite simple:

Copy the following class:

public class TestNGReportAppender extends AppenderSkeleton {

  @Override
  protected void append(final LoggingEvent event) {
    Reporter.log(eventToString(event));
  }

  private String eventToString(final LoggingEvent event) {
    final StringBuilder result = new StringBuilder(layout.format(event));

    if(layout.ignoresThrowable()) {
      final String[] s = event.getThrowableStrRep();
      if (s != null) {
        for (final String value : s) {
          result.append(value).append(Layout.LINE_SEP);
        }
      }
    }
    return result.toString();
  }

  @Override
  public void close() {

  }

  @Override
  public boolean requiresLayout() {
    return true;
  }
}

and configure it just like a console appender. E.g. like this:

log4j.appender.testNG=some.package.TestNGReportAppender
log4j.appender.testNG.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.testNG.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%



回答2:


This post might also help you:

"Integrate commons-logging output with testNG test case reports"



来源:https://stackoverflow.com/questions/1574139/is-there-a-log4j-appender-that-connects-with-testng

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!