問(wèn)題描述
我希望在所有測(cè)試用例的 TestNG 報(bào)告中提供 Log4j2 日志記錄信息.
I would like to have the Log4j2 logging information available in the TestNG reports for all of the test cases.
TestNG 使用一個(gè)名為 Reporter.java 的特殊記錄器類,它跟蹤日志輸出并將其保存在其結(jié)果 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 中,可以簡(jiǎn)單地創(chuàng)建一個(gè)路由到 Reporter 并注冊(cè)它的 appender 實(shí)現(xiàn).
In log4j it was possible to simply create an appender implementation that routes to Reporter and register it.
使用 Log4j2 中的新 Logger API,很難找到有關(guān)如何完成此操作的信息.我有一些信息可以使用 Log4j 而不是使用 Log4j2 來(lái)完成.
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.
推薦答案
據(jù)我所知,你只需要實(shí)現(xiàn)一個(gè)簡(jiǎn)單的 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);
}
}
這篇關(guān)于如何在 TestNG 報(bào)告中包含 Log4j2 消息的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!