我对ZF3很新手,我无法弄清楚如何将记录器模块定义为服务,以及如何在其他模块中使用(重用)它。从这个角度来看,官方documentation很穷。任何简短的例子都会很好。
如果你想在ZF应用程序中使用zend-log,安装后你需要做两件事:
Zend\Log
。'log' => [
'MyLogger' => [
'writers' => [
'stream' => [
'name' => 'stream',
'priority' => \Zend\Log\Logger::ALERT,
'options' => [
'stream' => '/tmp/php_errors.log',
'formatter' => [
'name' => \Zend\Log\Formatter\Simple::class,
'options' => [
'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
'dateTimeFormat' => 'c',
],
],
'filters' => [
'priority' => [
'name' => 'priority',
'options' => [
'operator' => '<=',
'priority' => \Zend\Log\Logger::INFO,
],
],
],
],
],
],
],
],
之后,只需从服务管理器获取并使用它:
$logger = $container->get('MyLogger'); // <-- the key that you register in config above
$logger->info('Logging info message in the file');
您可能希望从SM获取记录器,而不是将其注入要使用它的类中。
有关于使用zend-log进行记录的神博文