How to enable Log4j2 messages in TestNG reports

I would like to have Log4j2 log information available in TestNG reports for all test cases.

TestNG uses a special log class called Reporter.java that tracks the output of the log and stores it in its XML results.

In log4j, you could simply create an appender implementation that is routed to Reporter and register it.

With the new Logger API in Log4j2, it was hard to find information on how to do this. I have some information to do this using Log4j, but not with Log4j2.

+5
source share
2 answers

From what I can say, you just need to implement a simple Appender. Sort of:

@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); } } 
+2
source
 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 

add this to pom.xml

 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>compile</scope> </dependency> 

then you can use log.info or log.error etc.

-2
source

Source: https://habr.com/ru/post/1205160/


All Articles