问题描述
我正在注册一个用于异常的Zend\Log实例,我将最终通过电子邮件发送所有系统错误,现在它只是一个文件。但是,它在控制器中不起作用,异常会在视图中显示(或不是,取决于 display_exceptions
)。我发现这个,没有人似乎很在乎。所以我需要一个解决方法。有没有办法使控制器不要吃我的例外? 'service_manager'=>数组(
'工厂'=>数组(
'Logger'=>函数($ sm)use($ sRootDir)
{
$ log = new Zend\ Log\Logger();
$ writer = new Zend\Log\Writer\Stream($ sRootDir。'/temp/license.log');
$ log-> addWriter $ writer
Zend\Log\Logger :: registerErrorHandler($ log);
Zend\Log\Logger :: registerExceptionHandler($ log);
return $ log;
},
),
您可以附加到发送错误事件:
Module.php
公共函数onBootstrap(MvcEvent $ e)
{
$ eventManager = $ e-> getApplication()
$ - > getEventManager();
$ moduleRouteListener = new ModuleRouteListener();
$ moduleRouteListener-> attach($ eventManager);
/ **
*记录任何未捕获的错误
* /
$ sharedManager = $ e-> getApplication() - > getEventManager() - > getSharedManager();
$ sm = $ e-> getApplication() - > getServiceManager();
$ sharedManager-> attach('Zend\Mvc\Application','dispatch.error',
function($ e)use($ sm){
if($ e - > getParam('exception')){
$ sm-> get('Logger') - > crit($ e-> getParam('exception'));
}
}
);
}
简单记录器的示例服务配置
'工厂'=> array(
'Logger'=> function($ sm){
$ logger = new \Zend\Log\Logger;
$ writer = new \Zend\Log \Writer\Stream('./ data / log /'。date('Ym-d').'- error.log');
$ logger-> addWriter($ writer);
return $ logger;
},
// ...
);
您还可以记录堆栈中的所有异常,以更好地了解出现的错误,而不是只显示最后一个例外,可能不包含很多信息。
public function onBootstrap(MvcEvent $ e)
{
$ eventManager = $ e-> getApplication() - > getEventManager();
$ moduleRouteListener = new ModuleRouteListener();
$ moduleRouteListener-> attach($ eventManager);
/ **
*记录任何未捕获的异常,包括堆栈中的所有异常
* /
$ sharedManager = $ e-> getApplication() - > getEventManager() - > getSharedManager();
$ sm = $ e-> getApplication() - > getServiceManager();
$ sharedManager-> attach('Zend\Mvc\Application','dispatch.error',
function($ e)use($ sm){
if($ e - > getParam('exception')){
$ ex = $ e-> getParam('exception');
do {
$ sm-> get('Logger' ) - > crit(
sprintf(
%s:%d%s(%d)[%s] \\\
,
$ ex-> getFile()
$ ex-> getLine(),
$ ex-> getMessage(),
$ ex-> getCode(),
get_class($ ex)
);
}
while($ ex = $ ex-> getPrevious());
}
}
);
}
I'm registering a Zend\Log instance for exceptions, I will need all system errors emailed in the end, now it's just goes to a file. However, it doesn't work in controllers, the exception gets displayed in the view (or not, depending on display_exceptions
). I found this bug, no one seems to care about it much. So I need a workaround. Is there a way to make controllers not to eat my exceptions?
'service_manager' => array(
'factories' => array(
'Logger' => function ($sm) use ($sRootDir)
{
$log = new Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream($sRootDir . '/temp/license.log');
$log->addWriter($writer);
Zend\Log\Logger::registerErrorHandler($log);
Zend\Log\Logger::registerExceptionHandler($log);
return $log;
},
),
You can attach to the dispatch error event:
Module.php
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/**
* Log any Uncaught Errors
*/
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
function($e) use ($sm) {
if ($e->getParam('exception')){
$sm->get('Logger')->crit($e->getParam('exception'));
}
}
);
}
An example service config for a simple logger
'factories' => array(
'Logger' => function($sm){
$logger = new \Zend\Log\Logger;
$writer = new \Zend\Log\Writer\Stream('./data/log/'.date('Y-m-d').'-error.log');
$logger->addWriter($writer);
return $logger;
},
// ...
);
You can also log all exceptions in the stack to get a better idea of what went wrong down the line, rather than only showing the last exception, which may not include much information.
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/**
* Log any Uncaught Exceptions, including all Exceptions in the stack
*/
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
function($e) use ($sm) {
if ($e->getParam('exception')){
$ex = $e->getParam('exception');
do {
$sm->get('Logger')->crit(
sprintf(
"%s:%d %s (%d) [%s]\n",
$ex->getFile(),
$ex->getLine(),
$ex->getMessage(),
$ex->getCode(),
get_class($ex)
)
);
}
while($ex = $ex->getPrevious());
}
}
);
}
这篇关于如何记录ZF2控制器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!