I want to inject a Log4j Logger (or in case of generality any class) into all my classes that has a log property:
def log
This is done in Grails automatically. I want to have the same feature in ordinary groovy apps, lets say for all groovy files under src
.
The speciality with Log4j is, that the logger needs to know the class which to log. (Logger.getLogger(Class clazz)
)
How can I achieve this?
Have you seen the @Log
annotation added in Groovy 1.8?
Groovy provides native support for this using the @Log4j
annotation on the class. This creates a log
field pointing to the Log4J Logger with the same name as the class in question, i.e.:
package com.example
import groovy.util.logging.Log4j
@Log4j
public class LogExample {
}
is the equivalent of
package com.example
public class LogExample {
private static final Logger log = Logger.getLogger(LogExample.class)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With