I would like to redirect out of println command which is details of job like Build name, status, time etc to another file.
Below is snippet of my code who's output i want to redirect to a file.
def lastBuildEnvVars = lastbuild.getEnvVars()
println 'Product Name : ' + lastBuildEnvVars['PRODUCT_NAME'] + 'Version : ' + lastBuildEnvVars['PRODUCT_VERSION'] + 'Result: ' + lastbuild.result + 'Time: ' + lastbuild.timestampString2 + '\n'
println 'Result: ' + lastbuild.result + '\nTime: ' + lastbuild.timestampString2
OutPut of println is
Current Product Name : OMA_KENAN_FX_READINESS
Product Version :
Result: SUCCESS
Time: 2019-08-31T01:25:26Z
def logFile = new File("output.log")
logFile.append('Product Name : ' + lastBuildEnvVars['PRODUCT_NAME'] + 'Version : ' + lastBuildEnvVars['PRODUCT_VERSION'] + 'Result: ' + lastbuild.result + 'Time: ' + lastbuild.timestampString2 + '\n')
logFile.append('Result: ' + lastbuild.result + '\nTime: ' + lastbuild.timestampString2)
You can assign a line to a string variable and then do:
def entry = 'Result: ' + lastbuild.result + '\nTime: ' + lastbuild.timestampString2
println(entry)
logFile.append(entry)
Then you could write a method called, say logIt(entry) that does the print and write to file:
def logIt(entry) {
//you have to let this method know about the file object somehow
println(entry)
logFile.append(entry)
}
and so on and so on.