如何在TestNG报告中包含Log4j2消息 [英] How to include Log4j2 messages in TestNG reporting

查看:459
本文介绍了如何在TestNG报告中包含Log4j2消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在TestNG报告中提供所有测试用例的Log4j2日志信息。

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

TestNG使用一个名为Reporter.java的特殊记录器类,它跟踪日志输出并将其保存在结果XML中。

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

在log4j中,可以简单地创建一个appender实现,路由到Reporter并注册它。

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

使用Log4j2中的新Logger API一直很困难找到有关如何完成此任务的信息。我有一些信息可以使用Log4j完成,但不能使用Log4j2。

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.

推荐答案

我可以告诉你只需要实现一个简单的Appender。类似于:

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);
    }
}

这篇关于如何在TestNG报告中包含Log4j2消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆