Donnerstag, 26. August 2010

log in a static method in Grails domain class

This post is in english, I guess it might help some people

I've been struggling with this issue some time. I found some hints via Google, especially a thread in Pubbs and one in Nabble. Both didn't help, though.

I don't know if what I did is good, or even "legal" or the Groovy way or... whatever, it works, though. Here on Grails 1.1.1 at least.

Here's the code:
def log = (new MyDomainClass()).log
It's so easy. I just instantiate a new object of the type of the current domain class and get its log property. That downsize is, it can't be copy-pasted to any domain class without editing it. Don't know if there's a solution to that.
For those who want a more talkative example:
class MyDomainClass{
static myStaticMethod () {
def log = (new MyDomainClass()).log
log.debug "This is a log message from a static domain method"
}
}
Did this help? Let me know in the comments.

Keine Kommentare: