This line should have been like this:
new
File(getClass().protectionDomain.codeSource.location.path).name.toLowerCase().replace('.groovy',
'')
Merlin Beedell
From: Merlin Beedell <[email protected]>
Sent: 26 January 2021 3:33 PM
To: [email protected]
Subject: Self logging scripts
Hi all,
I am successfully using the following chunk of groovy code at the top of
various console scripts in order to preserve the various stdout/strerr output
to a log file - as well as on the console.
There are a couple of points. First it uses SystemOutputInterceptor - but this
class appears to be depreciated. Is there something coming to replace it?
Second, I would like to be able to print the command line as entered by the
user who provoked the script. The nearest I could get was to print the args
list (as below). Is there a simple way of obtaining the actual command entered
by the user so it can be included with the log? I was hoping the CliBuilder
might have a pretty print feature, but I can't see one.
import groovy.ui.SystemOutputInterceptor
def dateString = new Date().format('yyyyMMMdd-HHmmss')
String getScriptFileName() {
new
File(getClass().protectionDomain.codeSource.location.path).name.toLowerCase().replace(/\.groovy/,
'')
}
new File('./logs').mkdir()
def logOutput = new PrintWriter(new
File("./logs/${getScriptFileName()}-${dateString}.log"))
def myLoger = new SystemOutputInterceptor( { Object... args -> logOutput <<
args[-1]; logOutput.flush(); true } )
myLoger.start()
if (args) {
print "Parameters provided:"
args.each {print " " + it}
println ""
}
println "this is printed to the console window and to the log file - like a
'tee' feature. Really useful"
Merlin Beedell