2012-10-11 141 views
0

我正在嘗試存儲一個實例或每個可審計域的id。例如,有一個域名爲'學生',並且是可審計的。因此,無論何時創建或編輯域的實例,都會在審覈日誌中存儲新記錄。我想要的是存儲每個保存或更新的域的ID。如何做呢 ? 在此先感謝Grails審計日誌記錄插件

回答

1

您可以使用下面使用Hibernate Envers的Grails Envers插件。插件信息可以在插件網站here上找到。

0

我不會使用審計插件,它在運行測試時有一些問題。這個插件是在GORM沒有提供事件支持時創建的。您可以攔截以下事件

beforeInsert - Executed before an object is initially persisted to the database 
beforeUpdate - Executed before an object is updated 
beforeDelete - Executed before an object is deleted 
beforeValidate - Executed before an object is validated 
afterInsert - Executed after an object is persisted to the database 
afterUpdate - Executed after an object has been updated 
afterDelete - Executed after an object has been deleted 
onLoad - Executed when an object is loaded from the database 

希望這有助於

+0

它的舊項目和審計日誌記錄已被使用,我不能改變這一點。有沒有辦法在審計日誌插件域中使用beforeInsert? –

+0

下面的內容應該有所幫助 – allthenutsandbolts

0

您可以使用審計日誌插件的事件,如下面,然後添加你想什麼都太。希望這是有道理

def onChange = { oldMap,newMap -> 
     println "Person was changed ..." 
     oldMap.each({ key, oldVal -> 
      if(oldVal != newMap[key]) { 
       println " * $key changed from $oldVal to " + newMap[key] 
      } 
     }) 
    } 


def onSave = { 
     println "new datainserted" 
     // may optionally refer to newState map 
    } 

在所有這些方法,我們可以使用

def event = new AuditLogEvent(
        actor: actor, 
        className: className, 
        eventName: eventName, 
        persistedObjectId: objectId, 
        propertyName: propertyName, 
        newValue: newValue 
      ) 
      if (event.validate()) { 
       auditLogListener.saveAuditLog(event) 
       log.info "Logged audit event [$event]" 
      }