0

I am using the grails 1.3.7 with audit logging plugin. I want to capture user login event like "user:PSam logged in at .." in the logs and since plugin does not have onLoad event defined, I am adding it to the domain classes and populate my own audit table entry in this event. So in grails user domain class I do as follows

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

I am having two issues here... 1) the username property defined in this domain class as an attribute is coming up as null 2) it throws an exception saying: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Is there another approach to be taken here? and also can use the plugins audi_log table to populated my onLoad() events entries?

Thanks in advance

4

1 回答 1

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())
      }
    }
}
于 2012-03-12T08:49:12.507 回答