1

I have a requirement to implement audit logging functionality in a zend project. The models are created using zend db and the update function is as follows.

public function updateGroup($data,$id)
{       
    $row = $this->find($id)->current();
    // set the row data
    $row->name                  = $data['name'];
    $row->description           = $data['description'];

    $row->updatedBy         = $data['updatedBy'];
    $row->updatedOn         = date('Y-m-d H:i:s'); 

    $id = $row->save();
    return $id;
}

I have to create a table with the auditlog information which includes the current userid. I have tried many methods and nothing is a good solution. What is the best practice for a good audit logging functionality for zend?

I just want to log only the modified data. and the log table schema is like

id, 
table, 
column,
rowId
oldvalue,
newvalue,
updatedon,
updatedbyuser 
codlib
  • 618
  • 1
  • 11
  • 28

1 Answers1

3

use Zend_Log_Writer_Db :

Zend_Log_Writer_Db writes log information to a database table using Zend_Db. The constructor of Zend_Log_Writer_Db receives a Zend_Db_Adapter instance, a table name, and a mapping of database columns to event data items

for example :

$columnMapping = array('name' => 'name', 
                       'desc' => 'desc', 
                       'updatedBy' => 'userid', 
                       'updatedOn' => 'date');
$writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping);

$logger = new Zend_Log($writer);


$logger->setEventItem('name', $data['name']);
$logger->setEventItem('desc', $data['name']);
$logger->setEventItem('updatedBy',$data['updatedBy']);
$logger->setEventItem('updatedOn',date('Y-m-d H:i:s'));

EDIT : to log only the modified data :

public function logUpdate(array $values)
{ 
    $columnMapping = array('id' => 'id', 
                           'table' => 'table', 
                           'column' => 'column',
                           'rowId' => 'rowId',
                           'oldvalue' => 'oldvalue',
                           'newvalue' => 'newvalue',
                           'updatedon' => 'updatedon',
                           'updatedbyuser' => 'updatedbyuser');

    $writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping);

    $logger = new Zend_Log($writer);


    $logger->setEventItem('id', $values['id']);
    $logger->setEventItem('table', $values['table']);
    $logger->setEventItem('column', $values['column']);
    $logger->setEventItem('rowId', $values['rowId']);
    $logger->setEventItem('oldvalue', $values['oldValue']);
    $logger->setEventItem('newValue', $values['newValue']);
    $logger->setEventItem('updatedon', $values['updatedon']);
    $logger->setEventItem('updatedbyuser', $values['updatedbyuser']);
} 

and in updateGroup :

public function updateGroup($data,$id)
{       
    $row = $this->find($id)->current();

    $values = array('table' => $this->name);
    $values = array('updatedon' => $data['updatedBy']);
    $values = array('updatedbyuser' => date('Y-m-d H:i:s'));
   //go through all data to log the modified columns
    foreach($data as $key => $value){
      //check if modified log the modification
      if($row->$key != $value){
        $values = array('column' => $key);
        $values = array('oldValue' => $row->$key);
        $values = array('newValue' => $value);
        logUpdate($values);
      }
    }

    // set the row data
    $row->name                  = $data['name'];
    $row->description           = $data['description'];

    $row->updatedBy         = $data['updatedBy'];
    $row->updatedOn         = date('Y-m-d H:i:s'); 

    $id = $row->save();


    return $id;
}

Note that its better to implement logging for all your application and seperate logging from update , see this answer for that .

Community
  • 1
  • 1
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Thanks for your answer. However this will write all information again to the db rigt? I just want to log only the modified data. and the log table schema is like id, table, column,oldvalue,newvalue,updatedon,updatedbyuser – codlib Feb 24 '12 at 07:36