我正在使用SOAPUI API为我的 SOAP Web 服务编写集成测试。
我的代码与下面的示例代码非常相似:
// create new project
WsdlProject project = new WsdlProject();
// import amazon wsdl
WsdlInterface iface = WsdlInterfaceFactory.importWsdl( "http://www.mycorp.com/somewsdl.wsdl", true )[0];
// get desired operation
WsdlOperation operation = (WsdlOperation) iface.getOperationByName( "MyOperation" );
// create a new empty request for that operation
WsdlRequest request = operation.addNewRequest( "My request" );
// generate the request content from the schema
request.setRequestContent( operation.createRequest( true ) );
// submit the request
WsdlSubmit submit = (WsdlSubmit) request.submit( new WsdlSubmitContext(), false );
// wait for the response
Response response = submit.getResponse();
// print the response
String content = response.getContentAsString();
System.out.println( content );
assertNotNull( content );
assertTrue( content.indexOf( "404 Not Found" ) > 0 );
运行此测试后,它会向控制台输出大量日志。有没有一种方法可以减少 SOAPUI 日志。
我在 jar 文件中拥有相关的 SOAP UI 代码及其各种依赖项。如果该项目使用常见的日志记录框架(我在依赖项中看到 log4j jar),是否有一种方法可以以编程方式设置日志记录级别?
类似的事情
project.setLoggingLevel("INFO");
最佳答案
我已经做到了。您必须创建两个log4j 配置文件,因为soapUI 使用Apache httpclient,它也使用log4j。 httpclient 使用默认的 log4j.xml 作为类路径资源加载。
soapUI 将尝试加载名为“soapui-log4j.xml”的 log4j 配置文件作为类路径资源(发生在 DefaultSoapUICore 类中)。
您在消息中说您无法更改 jar 中的 log4j.xml,因此您必须通过将新的 log4j.xml 放在类路径中的前面来覆盖它,以便首先找到它们。
这是我的 log4j.xml,用于保持 httpclient 日志记录较低:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console-apache" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyMMdd-HHmmss.SSS} [%t][%-5p] [%c] %m%n" />
</layout>
</appender>
<logger name="org.apache">
<level value="ERROR" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="console-apache" />
</root>
</log4j:configuration>
这是我的 log4j-soapui.xml:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console-soapui" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyMMdd-HHmmss.SSS} [%t][%-5p] [%c] %m%n" />
</layout>
</appender>
<logger name="com.eviware">
<level value="INFO" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="console-soapui" />
</root>
</log4j:configuration>
最后,您可以通过定义几个定义配置文件位置的系统属性来覆盖任何默认加载行为:
-Dsoapui.log4j.config=${yourpath}/log4j-soapui.xml
-Dlog4j.xml=${yourpath}/log4j.xml
关于java - 减少 SOAPUI API 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199129/