This module is based on [alexanderstrebkov / auditlog module] (https://github.com/alexanderstrebkov/auditlog)
A few features have been added to allow more control on what to audit.
- An annotation (@play.modules.auditlog.Auditable) that allows to mark only the modules that you want audit logs for
- The annotation recordOn field to give you even more control for what operations you want to log.
- A way to specify how to get the current logged user (actor)
To use the module you can clone this repo and add it to your repositories .In dependencies.yml append:
- auditlog -> auditlog
repositories:
- My modules:
type: local
artifact: ${application.path}/../[module]
contains:
- auditlog
Then you need to implement the interface IActorProvider to tell auditlog module who is the current user. You just need to implement it, then the module will find it.
A simple implementation would look like this
package utils;
import models.Event;
import play.db.jpa.JPABase;
import play.modules.auditlog.IActorProvider;
import play.mvc.Scope.Session;
public class AuditLogActorProvider implements IActorProvider {
@Override
public String getActor() {
if (Session.current() != null && Session.current().get("username") != null){
return Session.current().get("username") ;
} else { //user not logged in
String tempUser = Session.current().get(Parameter.TEMP_USER);
if (tempUser != null){
return tempUser;
}
}
throw new IllegalArgumentException("Cannot find the actor for this action. Will have to skip this log");
}
}
The usage is quite simple. Let's say you have a model class called Comment and you only want to log when a comment is created.
@Auditable(recordOn={Operation.CREATE})
@Entity
public class Comment extends Model {
...
}
If you want to record create and updates then you would use
@Auditable(recordOn = {Operation.CREATE, Operation.UPDATE})
well you got the idea.
Also you will need to add the following properties in the application.conf file
# audit - log
hibernate.ejb.event.post-insert=play.modules.auditlog.AuditLogListener
hibernate.ejb.event.post-update=play.modules.auditlog.AuditLogListener
hibernate.ejb.event.post-delete=play.modules.auditlog.AuditLogListener