How to handle updating domain by few lines of code

with No Comments

We will be use audit­log grails plugin, that based on Hibernate inteceptor.

The lastest version is org.grails.plugins:audit­logging:1.1.0

So I’ll use it.

The pretty thing that we can start using it in 2 easy steps:

  1. Added plugin to buildConfig
  2. Choose the domains classes that we want to log

First step I think clear for everyone who work with grails, second step is easy too:

for example we have class User:

And we want to log changing phoneNumber and city fields.

So we add:
static auditable = [ignore: ['version', 'lastUpdated', 'username'', 'password']]
After updating class will be:

If you want to add all fields to audit you should instead
static auditable = [ignore...]
static auditable = true
All fields exclude version and lastUpdated will be logable.

If you want to log version and lastUpdated fields too, you should add:
static auditable = [ignore:[]]
By default loging will be log per changing field, if you want to do log per changing object you should add to config.groovy:

Finally I want to show how it will look:

id, dateCreated and lastUpdate– I think it clear.
actor– who updated object(current user if you use spring security or system)
className– Class changed object
persistedObjectId – id changed object
persistedObjectVersion – current version
eventName– Can be UPDATE, INSERT, DELETE
propertyName– name of field that was changed
oldValue– old value
newValue– new value

That’s all that you need to know that start using audit log on grails.

If someone will be interest in it, I can explain this theme more depper :)

Leave a Reply