Id like to add a field that contains the user id from the logged in user into the zend db logger
.
Here is my current code:
$mapping = array(
'timestamp' => 'created',
'priorityName' => 'type',
'message' => 'message'
);
$dbAdapter = Pluto::database()->getMasterAdapter();
$writer = new \Zend\Log\Writer\Db($dbAdapter,'logs',$mapping);
$this->logger->addWriter($writer);
My userid column will be userid
. If the user is not logged in then it should insert 0.
Not quite sure what to do here.
UPDATE
Here's my current processor:
namespace Pluto\Log;
use Zend\Log\Processor\ProcessorInterface;
class UserAwareProcessor implements ProcessorInterface
{
/**
* @param array $event event data
* @return array event data
*/
public function process(array $event)
{
if (! isset($event['extra'])) {
$event['extra'] = [];
}
$event['extra']['userid'] = 5;
return $event;
}
}
You can use a custom log processor to easily append/prepend any data to your logs just before writing. Since processors are called from the logger before the log event is passed to the writer, I would create MyApp\\Log\\UserAwareProcessor
with a following signature and body:
<?php
namespace MyApp\Log;
use Zend\Log\Processor\ProcessorInterface
class UserAwareProcessor implements ProcessorInterface
{
/**
* @param array $event event data
* @return array event data
*/
public function process(array $event)
{
if (! isset($event['extra'])) {
$event['extra'] = [];
}
$event['extra']['userId'] = $this->getUserId();
return $event;
}
/**
* @return int
*/
private function getUserId()
{
// Read the id from session or any other source and return it
}
}
At the end, after setting a writer to your logger, you'll need also set the processor something like:
$this->logger->addWriter($writer);
$processor = new MyApp\Log\UserAwareProcessor();
$this->logger->addProcessor($processor);
You may also want to checkout log formatters .
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.