Science
Science

Reputation: 147

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?

Upvotes: 0

Views: 108

Answers (2)

Achim
Achim

Reputation: 322

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.

Upvotes: 0

Rodin Vasiliy
Rodin Vasiliy

Reputation: 583

Briefly: declare own logger with dependency from AuthenticationService, override log method, use $extra variable to store additional data

In detail:

Application\Log\LoggerFactory.php

class LoggerFactory implements FactoryInterface
{
    protected $mapping = array(
        // Your mapping here
    );

    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $authenticationService = $serviceLocator->get('Zend\Authentication\AuthenticationService');

        $dbAdapter = $serviceLocator->get('Zend\Db\Adapter\StatisticAdapter');
        $writer = new DbWriter($dbAdapter, 'error_log_table', $this->mapping);

        $logger = new Logger();
        $logger->addWriter($writer);

        $logger->setAuthenticationService($authenticationService);

        return $logger;
    }

}

Application\Log\Logger.php

class Logger extends \Zend\Log\Logger
{

    protected $authenticationService;

    public function setAuthenticationService($authenticationService)
    {
         $this->authenticationService = $authenticationService;
    }

    public function log($priority, $message, $extra = array())
    {

        $remoteAddress = new RemoteAddress;
        $ip = $remoteAddress->setUseProxy(true)->getIpAddress();   

        $userId = null;
        if ($this->authenticationService->hasIdentity()) {
             $userId = $this->authenticationService->getIdentity()->getId();
        } 

        $extra = array_merge($extra, array(
            'ip' => ip2long($ip),
            'user_id' => $userId
        ));

        return parent::log($priority, $message, $extra);
    }

module.config.php

'service_manager' => array(
    'factories' => array(
        'Zend\Log' => 'Application\Log\LoggerFactory',
    ),

Your controller

$this->getServiceLocator()->get('Zend\Log')->info('Info');

Upvotes: 0

Related Questions