无法取得在ZF2记录器服务(Can't get Logger service in ZF2)

2019-10-19 16:42发布

我挣扎的问题数天,但仍然没有找到什么地方错了。

这里是我的application.config.php配置段:

// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Log\LoggerAbstractServiceFactory',
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
    )
),

'log' => array(
    'Log\App' => array(
        'writers' => array(
            array(
                'name' => 'stream',
                'priority' => 1000,
                'options' => array(
                    'stream' => 'data/logs/app.log'
                )
            )
        )
    )
)

我试图让服务实例在我的控制器:

$logger = $this->getServiceLocator()->get('Log\\App');

并抛出异常:

An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\ServiceManager\Exception\ServiceNotFoundException
File:
/var/openresty/html/skeleton-zf2/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:529
Message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Log\App

更新谢谢你的帮助ocramius

我谨段module.config.php,它抛出的异常此时:

Zend\ServiceManager\Exception\ServiceNotCreatedException
File:
/var/openresty/html/skeleton-zf2/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:1070
Message:
An abstract factory could not create an instance of applicationweb(alias: Application\Web).

Answer 1:

作为IRC上讨论的,这里的问题是2:

  • 在这里你有这个配置文件是config/application.config.php (这是不是模块/合并的配置,但什么被放入服务"ApplicationConfig"
  • 抽象工厂不能因异常实例化的服务。 为了调试例外,你可以这样做如下:
try {
    // ...
    $logger = $this->getServiceLocator()->get('Log\\App');
    // ...
} catch (\Exception $e) {
    do {
        var_dump($e->getMessage());
    } while ($e = $e->getPrevious());
}

最可能的是,你的配置有以前由日志写入访问的文件错误的道路,那将拿出由像我刚刚表明跟踪这些异常。



文章来源: Can't get Logger service in ZF2