Grails审计日志插件

时间:2012-10-11 10:19:43

标签: grails logging audit

我正在尝试存储可审核的每个域的实例或至少id。例如,有一个域'学生'并且是可审核的。因此,每当域创建或编辑域实例时,新记录都存储在审计日志中。我想要的是存储保存或更新的每个域的ID。怎么做到呢 ? 提前致谢

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您最好参考网址:http://grails.org/plugin/audit-logging

上面的插件适用于没有集合的域类,如果您在域类中使用集合,请参阅此stackoverflow链接:How to determine Collection changes in a Hibernate PostUpdateEventListener?

希望它会有很多帮助

答案 2 :(得分: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

希望这有帮助

答案 3 :(得分:0)

您可以将事件与AuditLog插件一起使用,如下所示,然后添加您想要的内容。希望这是有道理的

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]"
            }