2012-03-11 89 views
0

我正在使用審計日誌插件grails 1.3.7。我想在日誌中捕獲用戶登錄事件,例如「user:PSam logged in ..」,由於插件沒有定義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()) 
     } 
    } 

我這裏有兩個問題... 1)在該領域類作爲屬性定義的用戶名屬性上來爲空 2)拋出一個異常說:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

這裏還有其他的方法嗎? 也可以使用插件的audi_log表填充我的onLoad()事件條目?

在此先感謝

回答

1

裹yoyr邏輯交易:

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()) 
     } 
    } 
} 
+0

非常感謝,這是這個問題,並解決它... – 2012-03-12 18:21:33