Php Can';无法在ZF2中获取记录器服务

Php Can';无法在ZF2中获取记录器服务,php,logging,zend-framework2,Php,Logging,Zend Framework2,我为这个问题挣扎了好几天,但仍然没有找到问题所在 下面是我在application.config.php中的配置部分: // Initial configuration with which to seed the ServiceManager. // Should be compatible with Zend\ServiceManager\Config. 'service_manager' => array( 'abstract_factories' => array(

我为这个问题挣扎了好几天,但仍然没有找到问题所在

下面是我在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
已更新 感谢您的帮助,奥克拉米乌斯

我将该段移动到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).

如IRC所述,这里的问题有2个:

  • 您进行此配置的文件是
    config/application.config.php
    (它不是模块/合并配置,而是投入服务的内容
    “ApplicationConfig”
  • 由于异常,抽象工厂无法实例化服务。要调试异常,可以执行以下操作:

最有可能的是,您的配置有一个错误的路径,该文件必须由日志编写器访问,并且将通过跟踪这些异常(如我刚才所示)出现。

您能否验证是否调用了
Zend\log\LoggerAbstractServiceFactory\canCreateServiceWithName()
?另外,您是指
application.config.php
还是
config/autoload
中的内容?最后解决它,谢谢。开始时,有点奇怪,将配置放错了application.config.php中。然后目录被selinux禁止。
try {
    // ...
    $logger = $this->getServiceLocator()->get('Log\\App');
    // ...
} catch (\Exception $e) {
    do {
        var_dump($e->getMessage());
    } while ($e = $e->getPrevious());
}