How can I make custom log (using Zend2) and set username to every log event. Here is my example how I`m trying:
public function log( )
{
$log_file = 'path/to/logfile.log';
$format = '%timestamp% %priorityName% (%username%) %message%';
$formatter = new Zend\Log\Formatter\Simple($format);
$formatter->setDateTimeFormat('H:i:s');
$formatter->format( array('username'=> SET_USER_NAME ));
$logWriter = new LogWriter($log_file);
$logWriter->setFormatter($formatter);
$logger = new Logger;
$logger->addWriter( $logWriter );
return $logger;
}
I tried the following
$this->log()->info(" Log message " );
And result is:
11:33:54 INFO (%username%) Log message
Thanks!
You can use third parameter of log method
extra
- array of extra data, which are converted to json and automatically added to log message by formatter (Zend\Log\Formatter\Simple::format)Simple example:
Outputs something like this: