zend db的审核日志功能

时间:2012-02-24 07:06:11

标签: zend-framework

我需要在zend项目中实现审计日志记录功能。模型使用zend db创建,更新功能如下。

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;
}

我必须创建一个包含审计日志信息的表,其中包含当前的用户ID。我尝试了很多方法,没有什么是好的解决方案。为zend提供良好的审计日志记录功能的最佳做法是什么?

我只想记录修改后的数据。并且日志表架构就像

id, 
table, 
column,
rowId
oldvalue,
newvalue,
updatedon,
updatedbyuser 

1 个答案:

答案 0 :(得分:3)

使用Zend_Log_Writer_Db

  

Zend_Log_Writer_Db使用将日志信息写入数据库表   Zend_Db的。 Zend_Log_Writer_Db的构造函数接收一个   Zend_Db_Adapter实例,表名和数据库映射   列到事件数据项

例如:

$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'));

编辑:仅记录修改过的数据:

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']);
} 

并在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;
}

请注意,最好为所有应用程序实施日志记录并从更新中分离日志记录,请参阅this answer