无法在ZF2中获得Logger服务 [英] Can't get Logger service in ZF2
问题描述
我为这个问题苦苦挣扎了好几天,但仍然没有发现问题所在.
I struggled the problem for days, but still don't find what's wrong with it.
这是我在application.config.php中的配置段:
Here is my configuration segment in 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'
)
)
)
)
)
,然后尝试在控制器中获取服务实例:
and I try to get the service instance in my controller:
$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,这次它抛出了异常:
I move the segment to module.config.php, and it thrown exception this time:
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:
As discussed on IRC, the problems here were 2:
- 具有此配置的文件是
config/application.config.php
(不是模块/合并配置的 ,但可以用于服务"ApplicationConfig"
的文件) - 由于异常,抽象工厂无法实例化服务.为了调试异常,您可以执行以下操作:
- the file where you had this configuration was
config/application.config.php
(which is not the module/merged configuration, but what gets put into service"ApplicationConfig"
) - the abstract factory couldn't instantiate a service due to an exception. In order to debug the exception, you can do something like following:
try {
// ...
$logger = $this->getServiceLocator()->get('Log\\App');
// ...
} catch (\Exception $e) {
do {
var_dump($e->getMessage());
} while ($e = $e->getPrevious());
}
很可能,您的配置为日志编写者必须访问的文件提供了错误的路径,并且通过跟踪这些异常(如我刚才所示)来显示.
Most probably, your configuration has a wrong path for the file that has to be accessed by the log writer, and that will come up by tracing these exceptions like I've just shown.
这篇关于无法在ZF2中获得Logger服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!