我正在将grails 1.3.7与审核日志记录插件一起使用。我想在日志中捕获用户登录事件,例如“user.PSam登录。”,并且由于插件未定义onLoad
事件,因此我将其添加到域类中,并在此事件中填充自己的审核表条目。
所以在grails用户域类中,我做如下
def onLoad = {
try {
Graaudit aInstance = new Graaudit();
aInstance.eventType= "User Log in"
aInstance.eventDescription = "User logged in"
aInstance.user_id = username
aInstance.save(flush:true);
println "username="+username
println aInstance;
}
catch (e){
println(e.getMessage())
}
}
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
最佳答案
在事务中包装逻辑:
def onLoad = {
Graaudit.withTransaction {
try {
Graaudit aInstance = new Graaudit();
aInstance.eventType= "User Log in"
aInstance.eventDescription = "User logged in"
aInstance.user_id = username
aInstance.save();
println "username="+username
println aInstance;
}
catch (e){
println(e.getMessage())
}
}
}
https://stackoverflow.com/questions/9659174/