Zend 3 - 使用记录器作为服务

问题描述 投票:1回答:1

我对ZF3很新手,我无法弄清楚如何将记录器模块定义为服务,以及如何在其他模块中使用(重用)它。从这个角度来看,官方documentation很穷。任何简短的例子都会很好。

php zend-framework3 zend-log
1个回答
4
投票

如果你想在ZF应用程序中使用zend-log,安装后你需要做两件事:

  1. 在'modules'下的应用程序配置中注册Zend\Log
  2. 在global.php或模块配置中为记录器添加配置 '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进行记录的神博文

© www.soinside.com 2019 - 2024. All rights reserved.