Been searching here and there looking for a working example of log4j logging to file in a Groovy script.
No explicit class (it's just a script). No grails. Not to console...to file.
Just a plain groovy script with log4j.
Is log4j best for this (groovy) or are other logging libraries better?
Can someone either point me to an example or demo how this is done? I managed to get it to console, but not to file.
Would be nice if the log4j configuration was in a config.groovy file as well, since I am using a config file for other things.
UPDATE
Thanks to neversleepz example, I have the following working nicely:
config.groovy file:
log4j {
appender.stdout = "org.apache.log4j.ConsoleAppender"
appender."stdout.layout"="org.apache.log4j.PatternLayout"
appender.scrlog = "org.apache.log4j.FileAppender"
appender."scrlog.layout"="org.apache.log4j.PatternLayout"
appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"
appender."scrlog.file"="rootscript.log"
rootLogger = "debug,scrlog,stdout"
}
And my script:
import org.apache.log4j.*
import groovy.util.logging.*
def config = new ConfigSlurper().parse(new File('config.groovy').toURL())
PropertyConfigurator.configure(config.toProperties())
Logger log = Logger.getInstance(getClass())
// Need to set log level as described here:
// http://groovy.329449.n5.nabble.com/log4j-annotation-not-working-td4368806.html
log.level = Level.INFO
// this will NOT print/write as the loglevel is info
log.debug 'Executing Script.'
// this will print
log.info 'Simple sample to show log INFO field is injected.'
log.warn 'Simple sample to show log WARN field is injected.'
log.error 'Simple sample to show log ERR field is injected.'
Thanks for this!
I've also configured for a RollingFileAppender, and DailyRollingFileAppender I'll put those here as well:
log4j {
//
appender.stdout = "org.apache.log4j.ConsoleAppender"
appender."stdout.layout"="org.apache.log4j.PatternLayout"
//
appender.scrlog = "org.apache.log4j.DailyRollingFileAppender"
appender."scrlog.DatePattern"="'.'yyyy-MM-dd"
appender."scrlog.Append"="true"
appender."scrlog.File"="rootscript.log"
appender."scrlog.layout"="org.apache.log4j.PatternLayout"
appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"
rootLogger="debug,scrlog,stdout"
}
and
log4j {
//
appender.stdout = "org.apache.log4j.ConsoleAppender"
appender."stdout.layout"="org.apache.log4j.PatternLayout"
//
appender.scrlog = "org.apache.log4j.DailyRollingFileAppender"
appender."scrlog.DatePattern"="'.'yyyy-MM-dd"
appender."scrlog.Append"="true"
appender."scrlog.File"="rootscript.log"
appender."scrlog.layout"="org.apache.log4j.PatternLayout"
appender."scrlog.layout.ConversionPattern"="%d %5p %c{1}:%L - %m%n"
rootLogger="debug,scrlog,stdout"
logger.ProcessLogger="debug,scrlog"
}
Here's a simple example using
@Grab
to pull in the log4j lib, then using the inbuilt Groovy's@Log4j
annotation to wire in alog
variable.You can then configure your logger in the script and add a
FileAppender
When run you'll see this in your myscript.log
Unfortunately, the config.groovy file logger configuration is specific to Grails. Since you aren't in Grails, the DSL isn't there to interpret the log4j stuff. However take a look at ConfigSlurper in Groovy that looks like it will provide you a similar DSL to bring in your config.
Another way to do this would be to use Groovy's logging package. You can use the same easily my importing the util logging and using the @Log annotation like in this example
However, since the annotation works on a class, you would need to create your groovy script inside a class. Free form scripts don't get the logger access this way.
Hope this helps.