vlambda博客
学习文章列表

【TestNG】如何让log4j2的日志展示在html report里

一. 背景

       很多时候我们的测试脚本里会引用一些公用的jar插件,插件用的log4j组件打印的日志。在测试报告里不能展示其日志,给我们排查日志造成一定的难度。


二. 改造logappender

      创建src/test/java/com/xyz/ReporterAppender.java

package com.xyz; import java.io.Serializable; import org.apache.logging.log4j.core.Layout;import org.apache.logging.log4j.core.LogEvent;import org.apache.logging.log4j.core.appender.AbstractAppender;import org.apache.logging.log4j.core.config.plugins.Plugin;import org.apache.logging.log4j.core.config.plugins.PluginAttribute;import org.apache.logging.log4j.core.config.plugins.PluginElement;import org.apache.logging.log4j.core.config.plugins.PluginFactory;import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;import org.apache.logging.log4j.core.layout.AbstractStringLayout;import org.testng.Reporter; @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); }}


三. log4j2配置文件(最主要的是要指定刚才创建的package,其它可按照实际情况修改)

       创建src/test/resources/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?><Configuration status="trace" packages="com.xyz"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg"/> </Console> <File name="FileAppender" fileName="../logs/TestNG_${date:yyyyMMdd_HHMMSS}.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg"/> </File> <Reporter name="TestReporter"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level: %msg"/> </Reporter> </Appenders>  <Loggers> <Root level="trace"> <AppenderRef ref="Console"/> <AppenderRef ref="FileAppender"/> <AppenderRef ref="TestReporter"/> </Root> </Loggers></Configuration>


四. 测试脚本

       创建src/test/java/com/xyz/tests/LoggingTest.java

package com.xyz.tests; import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.testng.annotations.Test; public class LoggingTest { private static final Logger logger = LogManager.getLogger();  @Test public void Test() { logger.info("Begin test."); logger.debug("Debug message"); logger.error("Error message."); logger.trace("Trace message."); }}


五. 执行测试脚本(这里采用的reportng)

    


六. 写在最后

      为了html报告的美观及可读性,可以再定制一下reportng,用颜色区分一下日志级别。