Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy Script and log4j

Tags:

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"
}
like image 419
Bean Avatar asked Nov 08 '13 20:11

Bean


Video Answer


2 Answers

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.

like image 68
neversleepz Avatar answered Sep 24 '22 13:09

neversleepz


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

import groovy.util.logging.*

@Log
class Test{
    public class() {
        log.debug "Logging"
    }
}

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.

like image 24
bythe4mile Avatar answered Sep 22 '22 13:09

bythe4mile