Чтобы ответить на мой собственный вопрос:
1.config.php
Файл конфигурации
/..
'controllers' => [
'factories' => [
Controller\LogController::class => Controller\LogControllerFactory::class,
/..
],
'aliases' => [
'log' => Controller\LogController::class,
/..
]
],
/..
'service_manager' => [
'factories' => [
'log' => Service\Log::class,
],
],
/..
2.Module.php
Файл модуля
namespace Application;
use Zend\Mvc\MvcEvent;
class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
// Get shared service manager
$services = $event->getApplication()->getServiceManager();
$sharedEventManager = $eventManager->getSharedManager();
// Attach listener
$sharedEventManager->attach('user', 'log-fail', function($event) use ($services) {
$username = $event->getParam('username');
$log = $services->get('log');
$log->warn('Error logging user: ' . $username);
});
}
}
3.LogService.php
Сервисный файл
namespace Application\Service;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Filter\Priority as PriorityFilter;
class Log implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$controllerPluginManager = $container;
$serviceManager = $controllerPluginManager->get('ServiceManager');
// Instantiate Zend\Log\Logger
$log = new Logger();
// Set stream to write in a log file
$writer = new StreamWriter('data/logs/audit.log');
$log->addWriter($writer);
return $log;
}
}
4.LogControllerFactory.php *
Заводской файл контроллера
namespace Application\Controller;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class LogControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$controllerPluginManager = $container;
$eventManager = $controllerPluginManager->get('EventManager');
$sharedEventManager = $eventManager->getSharedManager();
return new LogController($sharedEventManager);
}
}
5.LogController.php
Контроллер файловый
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;
class LogController extends AbstractActionController
{
public function __construct(SharedEventManager $sharedEventManager)
{
$this->_sharedEventManager = $sharedEventManager;
}
public function inAction()
{
//...
if (! $isValid) {
$event = new EventManager($this->_sharedEventManager, ['user']);
$event->trigger('log-fail', $this, array('username'=> $username));
//...
}
}
}