Multithreading ThreadID未打印在log4j日志中
我正在从maven pom.xml执行testng.xml,testng.xml中指定了threadcount=5。出于调试目的,我需要在日志中打印threadID,但无法这样做。以下是配置详细信息:-Multithreading ThreadID未打印在log4j日志中,multithreading,maven,selenium-webdriver,log4j,testng,Multithreading,Maven,Selenium Webdriver,Log4j,Testng,我正在从maven pom.xml执行testng.xml,testng.xml中指定了threadcount=5。出于调试目的,我需要在日志中打印threadID,但无法这样做。以下是配置详细信息:- Pom.xml ------- <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version>
Pom.xml
-------
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
Testng.xml
----------
<suite name="Functionality Test" parallel="tests"
verbose="5" thread-count="5" configfailurepolicy="continue" >
<test name="Industry Brief" >
<classes>
<class name="com.firstrain.XX" />
</classes>
</test>
<test name="FR Labs">
<classes>
<class name="com.firstrain.YY" />
</classes>
</test>
</suite>
Log4j.properties pattern Layout
-------------------------------
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
Current Output
-------------
INFO [TestNG] (XX.java:27) - testme XX
INFO [TestNG] (YY.java:25) - testme YY
INFO [TestNG] (YY.java:26) - 1234
INFO [TestNG] (XX.java:27) - testme XX again
INFO [TestNG] (YY.java:47) - testme YY again
Expected Output
-------------
INFO [pool-1-thread-1] (XX.java:27) - testme XX
INFO [pool-1-thread-2] (YY.java:25) - testme YY
INFO [pool-1-thread-2] (YY.java:26) - 1234
INFO [pool-1-thread-1] (XX.java:27) - testme XX again
INFO [pool-1-thread-2] (YY.java:47) - testme YY again
您可以使用以下行来区分并行执行中的线程
Thread.currentThread().getId()
或者您可以使用
Thread.currentThread().setName(name)
并使用
Thread.currentThread().getName()
你能发布你的测试代码吗?该输出看起来像是您的测试代码可能在单个线程中执行。我假设您的测试代码必须执行Thread.setName?Kharyam,谢谢您的回复。我没有在我的代码中任何地方使用Thread.setName…当我运行我的代码时,testng正在运行前面提到的5个并行线程进行测试。我甚至遇到了同样的问题。。。它总是显示TestNG,您永远无法确定哪个线程正在运行,这可能是因为TestNG在内部将每个线程的名称设置为“TestNG”是的,Rishi使用上述方法用测试类名重命名每个线程名。谢谢