7

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

TestNG uses a special logger class called Reporter.java that keeps track of the log output and saves it in its results XML.

In log4j it was possible to simply create an appender implementation that routes to Reporter and register it.

With the new Logger API in Log4j2 it has been difficult to find information on how to accomplish this. I have some information to get this done using Log4j but not with Log4j2.

BSMP
  • 4,596
  • 8
  • 33
  • 44
Praveen
  • 201
  • 5
  • 18
  • Did you google it? http://goo.gl/UyUqN4 – rinuthomaz Oct 21 '14 at 10:06
  • @rinuthomaz log4j2 does not seem to be the question. Integration with testng does. – AnthonyJClink Apr 22 '16 at 01:47
  • please be specific with test report, like what test report ? – Mehdi Apr 24 '16 at 09:39
  • 1
    looks like this SO answer might have what you need: http://stackoverflow.com/a/36210968/2234770 – snerd Apr 25 '16 at 16:42
  • This explains how to create a new log file with log4j I have already done this. Though the look was very appricated @DavidHoliday – AnthonyJClink Apr 26 '16 at 17:15
  • @rinuthomaz TestNG has a special logger that when messages are sent to it.. they are recorded automatically. Since both the OP and myself are trying to use other features of Log4j in conjunction with our tests we need this integration point. – AnthonyJClink Apr 26 '16 at 17:19
  • 2
    This answer might be of interest to you: [how to create a custom appender in log4j2](http://stackoverflow.com/questions/24205093/how-to-create-a-custom-appender-in-log4j2). – uniknow Apr 28 '16 at 09:37

2 Answers2

4

From what I can tell you just need to implement a simple Appender. Something like:

@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);
    }
}
rgoers
  • 8,696
  • 1
  • 22
  • 24
  • 1
    Agree, Ralph's answer deserves the bonus. – Remko Popma Apr 29 '16 at 02:02
  • great answer. but if anyone facing compilation issue for above code , then just fix line Reporter.log(event.getMessage().getFormattedMessage(); } to correct one as Reporter.log(event.getMessage().getFormattedMessage());}} // method cosing – Vikas Thange Apr 06 '23 at 03:12
-2
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

add this in 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...

Lokesh Sanapalli
  • 1,012
  • 3
  • 18
  • 39