Reputation: 2261
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?
Upvotes: 6
Views: 4316
Reputation: 122414
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)
}
Upvotes: 4