Bean Bean - 3 months ago 23
Groovy Question

Groovy Script and log4j

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"
}

Answer

Here's a simple example using @Grab to pull in the log4j lib, then using the inbuilt Groovy's @Log4j annotation to wire in a log variable.

You can then configure your logger in the script and add a FileAppender

@Grab('log4j:log4j:1.2.17')

import org.apache.log4j.*
import groovy.util.logging.*

@Log4j
class HelloWorld{
    def execute() {
        // Need to set log level as described here: 
        // http://groovy.329449.n5.nabble.com/log4j-annotation-not-working-td4368806.html
        log.level = Level.INFO
        // add an appender to log to file
        log.addAppender(new FileAppender(new TTCCLayout(), 'myscript.log'));

        // this will NOT print/write as the loglevel is info
        log.debug 'Execute HelloWorld.'
        // this will print
        log.info 'Simple sample to show log field is injected.'
    }
}

def helloWorld = new HelloWorld()
helloWorld.execute()

When run you'll see this in your myscript.log

    11 [main] INFO HelloWorld - Simple sample to show log field is injected.

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.