How to include Log4j2 messages in TestNG reporting(如何在 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 中,可以简单地创建一个路由到 Reporter 并注册它的 appender 实现.
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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 TestNG 报告中包含 Log4j2 消息


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01