Is there a log4j application that connects to TestNG?

I use log4j and would like the log messages that usually go to my logger to appear in the test reports generated by TestNG during my unit tests.

I think that would mean the Appender , which displays in the TestNG Listener and the corresponding log4j configuration in the src / test / resources directory of my Maven project. It is right?

It seems pretty easy to write, but is there something I can only do through Maven?

+4
source share
2 answers

I had the same problem, and in the end I myself coded the application. This 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. For instance. eg:

 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% 
+3
source
+1
source

All Articles