Question

For a Web-Application with many Database related events I want to build a Changelog. That should log what users have done, so its a Userlog too. The App is huge, has a complex role based user access system and there will be hundreds of different events (changes) that may occur.

This all should be Database-Driven, in PHP and needs at least a View to search the Logs.

But in short, I have totally no idea how to design that all and need some tips or inspirations, maybe what others have done.

Was it helpful?

Solution

I've done this in the past and found basically 2 approaches: Model-based and Controller-based.

  • Model-based within the model itself override the save and update methods (assuming an ActiveRecord pattern) to create a new entry in the ChangeLog table.

  • Controller-based add the ChangeLog record creation logic to each controller that you want to track.

I prefer the Controller-based approach since you have more control over what's going on and when. Also, you have full access to the user session so it's easier to add tracking for auditing purposes.

OTHER TIPS

Have solved that much more easy as it appears with my first thoughts. This should do it most times without a comment:

protected function log($comment = ''){
    $user = $this->user();
    ORM::factory('Changelog')->vaules(array(
        'user_id' => $user->pk(),
        'section_id' => $user->section->pk(),
        'username' => $user->username.'@'.$user->section->name,
        'time' => time(),
        'uri' => $this->uri($this->request->param(), $this->request->query()),
        'controller' => $this->request->controller(),
        'action' => $this->request->action(),
        'post' => serialize($this->request->post()),
        'comment' => $comment,
    ))->save();
}

A simple $this->log() and all is done.

Result: http://charterix.sourceforge.net/log.jpg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top