How to include Log4j2 messages in TestNG reporting

前端 未结 2 854
轻奢々
轻奢々 2021-02-09 20:20

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 t

相关标签:
2条回答
  • 2021-02-09 20:52

    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);
        }
    }
    
    0 讨论(0)
  • 2021-02-09 20:55
    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...

    0 讨论(0)
提交回复
热议问题