简体   繁体   中英

Logging using log4j in SoapUI

For the last hours I've tried to configure and use a custom logger using log4j in my SoapUI project. Generally my logging works without any problems, but I assume that log4j has some nice features, which would be nice to use.

Current approach looks like the following:

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")

This results in an output of:

-------Check httpStatus and ResponseTime-------
httpStatus of Request: 200
ResponseTime: 767ms.

Assumed the file gets bigger and biger and get thousands lines of text, I want to have an automatic process, where a new file will be created, if the file-size is bigger than 10MB. The new file should called log-1.txt . From my understanding, log4j is the perfect solution for my purpose. I've already searched for some instructions and found this: http://0guzhan.blogspot.de/2011/12/defining-custom-loggers-in-soapui.html

So...what I have done: I've edited the soapui-log4j.xml and I've added the following lines:

<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>

No I have created a new GroovyScript as a Testcase with the follwoing content:

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)
}

The result is that log.info("ResponseTime is: " + responseTime) get called, but nothing will be logged to any file. Also the file, which I've defined via <param name="File" value="ASSERTION.log"/> won't be created (not in SoapUI's bin-directory, not in the project-directory).

Do I miss anything? Any tipp is much appreciated!

I think that you're doing the things correctly, note only the follow things:

If you specify directly a value for the param <param name="File" value="ASSERTION.log"/> then the log creates the file in SOAPUI_HOME\\bin\\ directory. Note that you can declare the full path like: <param name="File" value="C:/temp/ASSERTION.log"/>

If you load the logger configuration in fileLogger object you can write to your log file using it object like fileLogger.info("FAULT: Response took:" + responseTime) not using log.info . I said this because in your question you said:

The result is that log.info("ResponseTime is: " + responseTime) get called, but nothing will be logged to any file.

Summarized I think that all your config is correct so I recommend you to simplify your groovy script as follows to make sure that the logs works correctly and it's not another thing who makes log goes wrong:

import org.apache.log4j.Logger 

def fileLogger = Logger.getLogger("ASSERTION.SCRIPT.LOGGER");
fileLogger.info("testing log");

Hope this helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM