Capturing Log4j output when executing TestNG tests
Use Reporter.log(str) to log the message on the report.
@AfterMethod
public void printLOGonFailure(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
String str = getLog();
Reporter.log(str);
}
}
This site has a explanation on how to do it.. I copied the code part here in case the link goes dead.
Logger.getLogger(this.getClass())
log4j.rootLogger=ERROR,TESTAPPENDER
log4j.appender.TESTAPPENDER=com.my.fantastic.MockedAppender
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class FooTest {
private Appender appenderMock;
@Before
public void setupAppender() {
appenderMock = mock(Appender.class);
Logger.getRootLogger().addAppender(appenderMock);
}
@After
public void removeAppender() {
Logger.getRootLogger().removeAppender(appenderMock);
}
@Test
public void testMethod() {
doStuffThatCausesLogging();
verify(appenderMock).doAppend((LoggingEvent) anyObject());
}
}
ArgumentCaptor arguments = ArgumentCaptor.forClass(LoggingEvent.class);
verify(appenderMock).doAppend(arguments.capture());
assertThat(arguments.getValue().getLevel(), is(Level.WARN));