在过去的几个小时里,我尝试在 SoapUI 项目中使用 log4j 配置和使用自定义记录器。一般来说,我的日志记录工作没有任何问题,但我认为 log4j 有一些很好的功能,使用起来会很好。
当前的方法如下所示:
File file = new File("C:\\Users\\doms\\Desktop\\log.txt")
file << ("-------" + "Check httpStatus and ResponseTime" + "-------")
file << ("\r\n")
//httpStatus
httpResponseHeader = messageExchange.responseHeaders
httpStatus = httpResponseHeader['#status#']
httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
log.info("httpStatus of Request: " + httpStatusCode)
file << ("httpStatus of Request: " + httpStatusCode )
file << ("\r\n")
//ResponseTime
responseTime = messageExchange.getTimeTaken()
log.info("ResponseTime: " + responseTime + "ms.")
file << ("ResponseTime: " + responseTime + "ms.")
file << ("\r\n")
这会产生以下输出:
-------Check httpStatus and ResponseTime-------
httpStatus of Request: 200
ResponseTime: 767ms.
假设文件变得越来越大并获得数千行文本,我想要一个自动过程,如果文件大小大于 10MB,将创建一个新文件。新文件应命名为log-1.txt。根据我的理解,log4j 是满足我的目的的完美解决方案。 我已经搜索了一些说明并发现了这个:http://0guzhan.blogspot.de/2011/12/defining-custom-loggers-in-soapui.html
所以...我做了什么:我编辑了 soapui-log4j.xml
我添加了以下几行:
<logger name="ASSERTION.SCRIPT.LOGGER">
<level value="INFO"/>
<appender-ref ref="ASSERTION"/>
</logger>
<appender name="ASSERTION" class="org.apache.log4j.RollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="File" value="ASSERTION.log"/>
<param name="Threshold" value="INFO"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
</layout>
</appender>
不,我已经创建了一个新的 GroovyScript 作为测试用例,其中包含以下内容:
import org.apache.log4j.Logger
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
// ASSERTION.SCRIPT.LOGGER is defined in soapui-log4j.xml as below
def fileLogger = Logger.getLogger("ASSERTION.SCRIPT.LOGGER");
log.info(fileLogger.class.name)
responseTime = testRunner.testCase.testSteps["myTestStep"].testRequest.response.timeTaken
// > 1 to get sure that I'll get in the if-statement.
if (responseTime > 1) {
//get sure that script goes into the if-clause by printing a line in SoapUIs's console
log.info("ResponseTime is: " + responseTime)
fileLogger.info ("FAULT: Response took:" + responseTime)
}
结果是log.info("ResponseTime is: " + responseTime)
被调用,但不会将任何内容记录到任何文件中。还有我通过 <param name="File" value="ASSERTION.log"/>
定义的文件不会被创建(不在 SoapUI 的 bin 目录中,不在项目目录中)。
我错过了什么吗?非常感谢您的任何提示!
最佳答案
我认为您做的事情是正确的,仅注意以下事项:
如果直接指定参数值 <param name="File" value="ASSERTION.log"/>
然后日志在 SOAPUI_HOME\bin\
中创建文件目录。请注意,您可以声明完整路径,例如: <param name="File" value="C:/temp/ASSERTION.log"/>
如果您在 fileLogger
中加载记录器配置您可以使用它的对象写入日志文件,例如 fileLogger.info("FAULT: Response took:" + responseTime)
不使用 log.info
。我这么说是因为在你的问题中你说:
The result is that log.info("ResponseTime is: " + responseTime) get called, but nothing will be logged to any file.
总结一下,我认为您的所有配置都是正确的,因此我建议您按如下方式简化您的 groovy 脚本,以确保日志正常工作,并且不会导致日志出错:
import org.apache.log4j.Logger
def fileLogger = Logger.getLogger("ASSERTION.SCRIPT.LOGGER");
fileLogger.info("testing log");
希望这有帮助,
关于在 SoapUI 中使用 log4j 进行日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28610193/