0

I see that grails-audit plugins provide way of logging onCreate() onUpdate() events, but I also need to track onLoad() events.

Is there any standard way to do it using any plugin? Or I need to implement it myself?

Milkywayfarer
  • 910
  • 1
  • 9
  • 25

1 Answers1

1

You can add onLoad closure to the domain class. It will be executed when the object is first loaded from the database.

class Foo {
    int bar;

    def onLoad = {
        // onLoad fired
    }
}
Andriy Budzinskyy
  • 1,971
  • 22
  • 28
  • is the onLoad event fired automatically when the domain objects load from the database. example : after user login occurs. Also I see that by default the audit_log table is updated when the event occurs, what If I want to customize or handle the insert myself or change the structure of the table, do I create another domain for this? – pri_dev Mar 10 '12 at 00:41
  • looks like onLoad closure since its user defined does not log entries into the audit_log table ... we have to create and manage our domain for this..? – pri_dev Mar 10 '12 at 00:47
  • how are you log onCreate() onUpdate() events to the audit_log table? – Andriy Budzinskyy Mar 12 '12 at 11:50
  • Hi, I am updating my own instance of Audit domain class in onCreate and onUpdate, I am not using onLoad,since its called many times and not just once. for example when a user logs into the application, using onLoad for logging creates 10 similar entries in the table - only difference is the microsecond in the timestamp. hence I create the audit log in the login controller onclick of submit button on login page and not in User domains onLoad. Also the default auditLog domain updated by the plugin saves only the class name, I need to get additional details like id saved/deleted etc? – pri_dev Mar 12 '12 at 18:14
  • Seems I don't understand the problem. You are tracking event in controller now - loading User domain. And now you want to track save/delete in this controller? Anyway, have a look at grails audit plugins, see [my comment](http://stackoverflow.com/a/9634387/1254417)? – Andriy Budzinskyy Mar 13 '12 at 08:59
  • @Andriy,what if I need to log each view access to specific domain objects? Not only when object is first loaded? Any standard way, or use own audit service, which will be called in show controller? – Milkywayfarer Jan 16 '13 at 11:51
  • I don't think if there is a way to do it inside domain objects. Here are all GORM events http://grails.org/doc/latest/guide/GORM.html#5.5.1%20Events%20and%20Auto%20Timestamping . I think you should use own audit service in controller. – Andriy Budzinskyy Jan 16 '13 at 15:00