2

I have to write Groovy script log in to a text file, I am able to write request and response of any Soap step using context.expand.

To write test request and response I am using:

def request =  context.expand('${SoapRequest#Request}')
new File ("D:/RequestFile.txt").write(request)

What I want is to save the log output in a file:

enter image description here

Suggest me a way to write Groovy script log in a text file.

albciff
  • 18,112
  • 4
  • 64
  • 89
Error Hunter
  • 1,354
  • 3
  • 13
  • 35
  • Do you want to save all logs (`log.info`,`log.warn` etc) from groovy testStep to a file? – albciff Sep 20 '16 at 21:51
  • SoapUI creates already log files namely `soapui.log, soapui-errors.log groovy-script.log`. Then why again anohter file? – Rao Sep 20 '16 at 22:33
  • @albciff - Yes , I added some logic in my Groovy script inside a test case of Soap UI. I am getting results of log.info and log.error in the script log. is there any method to copy the result – Error Hunter Sep 21 '16 at 05:04
  • @Rao - we can find soapui script log in bin folder but I want to create a new result file which contains result for one script – Error Hunter Sep 21 '16 at 05:07
  • Result of test case? What is your user case? Since you mentioned log, it is leading to confusion. By the way, what exactly you wish to write? Please update the details in the question. – Rao Sep 21 '16 at 05:11
  • I am using Soap UI to write Groovy scripting, for example I have created Soap UI TC and added Groovy script step(Not in script assertion). I have multiple log.info and log.error in script and once I am executing same getting result in log output(Groovy Script result).I wish to write this log to a file – Error Hunter Sep 21 '16 at 05:39
  • Not clear yet. Any way, check [this](http://stackoverflow.com/questions/4272330/groovy-write-to-file-newline) writing to file. – Rao Sep 21 '16 at 05:49
  • @Rao - [image](https://www.google.co.in/search?q=Soapui+Groovy+script&safe=active&espv=2&biw=1164&bih=813&source=lnms&tbm=isch&sa=X&ved=0ahUKEwi6_bKK5p_PAhXDGR4KHdk6CHQQ_AUIBigB#imgrc=AL38-VF878ZD_M%3A) Please see this image , Here I need to write output from this to a file – Error Hunter Sep 21 '16 at 06:11
  • @ErrorHunter I edit your question to make clear what you want... I hope it is correct, if not feel free to rollback my edit :). – albciff Sep 21 '16 at 07:56
  • @albciff thank you so much ..this is what I wanted to ask. I will try the below mentioned solution and will update if works – Error Hunter Sep 21 '16 at 08:28
  • @albciff this solution is working, However now I appending the required data directly to a file – Error Hunter Sep 23 '16 at 06:58
  • @ErrorHunter sorry, I don't understand your comment... what do you mean? – albciff Sep 23 '16 at 07:01
  • @ErrorHunter do you want to start with a empty file each time? – albciff Sep 23 '16 at 07:02
  • @albciff - I want to store output for each run, and if I am executing this again it should rewrite the file. – Error Hunter Sep 23 '16 at 07:21
  • @ErrorHunter I update my answer... but I'm still not sure if I understand correctly your requeriment... If the answer solves your problem thinks about [accept it](http://stackoverflow.com/help/accepted-answer) – albciff Sep 23 '16 at 10:54

1 Answers1

5

Config way log4j.xml

There is already a file for Groovy log configured in SOAPUI log4j configuration file.

In SOAPUI_HOME\bin\soapui-log4j.xml:

<appender name="GLOBAL_GROOVY_LOG" class="org.apache.log4j.FileAppender">
  <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
  <param name="File" value="${soapui.logroot}global-groovy.log"/>
  <param name="Threshold" value="DEBUG"/>
  <param name="Append" value="true"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
  </layout>      
</appender>

<logger name="groovy.log">
  <level value="INFO" />
  <appender-ref ref="GLOBAL_GROOVY_LOG" />
</logger>   

A possible way is to add another custom FileAppender in this config file. If you change this file remember to restart SOAPUI in order that it can load the changes.

Something like this can do the trick:

<appender name="GLOBAL_GROOVY_LOG" class="org.apache.log4j.FileAppender">
  <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
  <param name="File" value="${soapui.logroot}global-groovy.log"/>
  <param name="Threshold" value="DEBUG"/>
  <param name="Append" value="true"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
  </layout>      
</appender>

<appender name="MYLOG_CUSTOM" class="org.apache.log4j.FileAppender">
  <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
  <param name="File" value="/absoultePath/yourlogFile.txt"/>
  <param name="Threshold" value="DEBUG"/>
  <param name="Append" value="true"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/>
  </layout>      
</appender>

<logger name="groovy.log">
  <level value="INFO" />
  <appender-ref ref="GLOBAL_GROOVY_LOG" />
  <appender-ref ref="MYLOG_CUSTOM" />
</logger>   

Dinamically using Groovy

However it seems that you want to do it dynamically using Groovy script, hence you can use the follow code to get <logger name="groovy.log"> and add a FileAppender to it; in order that you can save the logs in a custom file:

import org.apache.log4j.Logger
import org.apache.log4j.PatternLayout
import org.apache.log4j.RollingFileAppender

// get the groovy logger by name
def groovyLogger = Logger.getLogger('groovy.log')

// pattern Layout
PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n")
// create a file appender
RollingFileAppender fileAppender = new RollingFileAppender(layout, "/absolutePath/myLog.txt")

groovyLogger.addAppender(fileAppender)

log.info 'someText to the logger'

NOTE: If you don't use an absolute path in FileAppender the log file is saved relative to *SOAPUI_HOME\bin*

When you configure this the rest of logs in Groovy testSteps will be appended to this file. If you want to get only the log for a specific Groovy testStep then you can simply remove the appender at the end of the script:

groovyLogger.removeAppender(fileAppender);

UPDATE:

From you comment: I want to store output for each run, and if I am executing this again it should rewrite the file.

So you want to store output for each run... but you want to overwrite the file if you execute again? This is contradictory isn't?

If you want to overwrite the file instead of append the content you can use setAppend(false):

fileAppender.setAppend(false)
fileAppender.activateOptions()
groovyLogger.addAppender(fileAppender)
albciff
  • 18,112
  • 4
  • 64
  • 89
  • I'm trying to use the groovy script to write logs to a custom log file, I can see that log file is getting created, but there are no logs in it. – i.am.jabi Jul 26 '21 at 10:09