You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Merlin Beedell <MB...@cryoserver.com> on 2021/01/26 15:33:06 UTC
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
0800 280 0525 / +44 (0)207 045 0520
DDI: +44 (0)207 045 0528
Mob: +44 (0)7876 226865
Cryoserver: A focused, flexible email archive delivered by experts
RE: Self logging scripts
Posted by Merlin Beedell <MB...@cryoserver.com>.
This line should have been like this:
new File(getClass().protectionDomain.codeSource.location.path).name.toLowerCase().replace('.groovy', '')
Merlin Beedell
From: Merlin Beedell <MB...@cryoserver.com>
Sent: 26 January 2021 3:33 PM
To: users@groovy.apache.org
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