Skip to content

Instantly share code, notes, and snippets.

@neversleepz
Created November 9, 2013 02:08
Show Gist options
  • Save neversleepz/7380662 to your computer and use it in GitHub Desktop.
Save neversleepz/7380662 to your computer and use it in GitHub Desktop.
Use log4j in a groovy script As asked in this <a href="http://stackoverflow.com/questions/19868180/groovy-script-and-log4j">stackoverflow question - groovy-script-and-log4j</a>
/**
* Use a Grails based Config.groovy to configure log4j
*
* As asked in this
* <a href="http://stackoverflow.com/questions/19868180/groovy-script-and-log4j">stackoverflow question - groovy-script-and-log4j</a>
*
*
* Tested with Java 7, Groovy 2.1.9
* User: kon
* Date: 9/11/13
* Time: 11:48 AM
*/
@Grab('log4j:log4j:1.2.17')
import org.apache.log4j.*
import groovy.util.logging.*
@Log4j
class HelloWorld{
def execute() {
// required 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 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment