Reputation: 31400
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?
Upvotes: 7
Views: 8960
Reputation: 40870
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%
Upvotes: 7
Reputation: 6099
This post might also help you:
"Integrate commons-logging output with testNG test case reports"
Upvotes: 1