phpzend-framework2zend-log

Issue with zend 2 log


I am currently working with zend 2 logger. I have a table error_log with the following fields:

And the following is my code

$db         = $this->service->get('Zend\Db\Adapter\Adapter');
$uId        = 0;

if ($auth->hasIdentity()) {
    $oId        = $auth->getIdentity();
    $uId        = $oId->user_id;
}

$mapping = array(
    'timestamp'     => 'timestamp',
    'priority'      => 'priority',
    'priorityName'  => 'priorityName',
    'message'       => 'message',
);
$writer = new \Zend\Log\Writer\Db($db, 'error_log_table', $mapping);
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

This works fine and the following fields will be updated. timestamp, priority, priorityName, & message.

I also want to update the user_id and ip field. How can i do this? (In zend 1, we can do this by using setEventItem() function). What function I have to call to add extra parameter in zend 2 log?

Somebody please help?


Solution

  • You can use $logger->info() with a second parameter "extra". This should be a Traversal, e.g. an array.

    Have a try with the following.

    $logger->info('message', array('user_id' => $uId, 'ip' => $_SERVER['REMOTE_ADDR']));
    

    I guess you have to add an additional database column "extra", where the extra logging event data is stored in.