June 23, 2016

Groovy Goodness: Customise Log AST Annotations


In the following example snippet we change the log field name to LOGGER and set a custom category name:

    @Grab(group='ch.qos.logback', module='logback-classic', version='1.1.7')
import groovy.util.logging.Slf4j

// Change name of the field to LOGGER and
// the category to ''.
@Slf4j(value = 'LOGGER', category = '')
class SampleLogging {

    String sample(final String message) { 'Running sample({}) method', message
        "Groovy is $message!"

def s = new SampleLogging()
println "System.out says: ${s.sample('gr8')}"

When we execute the script we get the following output:

16:33:56.972 [Thread-7] INFO - Running sample(gr8) method
System.out says: Groovy is gr8!

Notice the category is and we use the field LOGGER in our code.

Written with Groovy 2.4.7.