Grails injects a log object for each artifact, but these loggers are not accessible within static methods.
Here’s a quick-and-dirty code snippet sample that demonstrates how to log from a static method within domain class Account. The trick is to get the Logger by invoking org.apache.commons.logging.LogFactory.getLog(this):
import org.apache.commons.logging.LogFactory
static Account createOrFindByImei(String imei)
{
Account result = Account.findByImei(imei)
return result ? result :
saved(new Account(
imei:imei,
email:Login.getCurrentLogin().email,
name:"* New Account ($imei) created at " + new Date()))
}
private static Account saved(Account account)
{
if(account.save())
{
return account
}
LogFactory.getLog(this)
.error("!saved: $account.errors", new Throwable("*STACKTRACE*"))
}
That’s useful. Worked for me. Thanks for sharing.
LikeLike