2017-03-08 19 views
0

我有一个ZF 3应用程序。我明确记录的信息例如$log->debug()显示得很好。例外情况不。错误似乎显示,因为这是默认的PHP配置去标准错误。下面是从modules.config.php相关线路:我是否正确配置Zend-Log的错误处理程序和异常管理器?

'service_manager' => [ 
    'factories' => [ 
     . . . . 
     'log' => \Zend\Log\LoggerServiceFactory::class, 

    ], 
], 
'log' => [ 
    'writers' => [ 
     [ 
      'name' => 'stream', 
      'options' => [ 'stream' => 'php://stderr' ] 
     ], 
    ], 
    'errorHandler' => true, 
    'exceptionhandler' => true, 
], 

The lines in the source that lead me to believe this is the correct config

if (isset($options['exceptionhandler']) && $options['exceptionhandler'] === true) { 
     static::registerExceptionHandler($this); 
    } 

    if (isset($options['errorhandler']) && $options['errorhandler'] === true) { 
     static::registerErrorHandler($this); 
    } 

为了测试它,我做了如下endpouints:

public function errorAction() 
{ 
    $msg = $this->params()->fromQuery('msg', 'Default Error message'); 
    trigger_error('Index Error Action' . $msg, E_USER_ERROR); 
    $model = new JsonErrorModel(['msg' => $msg]); 
    return $model; 
} 

public function exceptionAction() 
{ 
    $msg = $this->params()->fromQuery('msg', 'Default Error message'); 
    throw new \RuntimeException('Index Exception Action' . $msg); 
    $model = new JsonErrorModel(['msg' => $msg]); 
    return $model; 
} 

回答

1

您的配置阵列中有错字

'log' => [ 
    .... 
    'errorHandler' => true, 
    .... 
], 

这个指数不应该驼峰应该errorhandler(全字母是小写的)。我还会添加fatal_error_shutdownfunction => true进行配置,以便记录致命错误。

Zend使用set_exception_handler来处理异常,所以请记住,日志记录异常只有在它们不在try/catch块中时才会起作用。

设置默认的异常处理程序,如果一个异常没有一个try/catch块中捕获的
来源:http://php.net/manual/en/function.set-exception-handler.php

所有这些功能可以手动设置:

\Zend\Log\Logger::registerErrorHandler($logger); 
\Zend\Log\Logger::registerFatalErrorShutdownFunction($logger); 
\Zend\Log\Logger::registerExceptionHandler($logger); 

如果你想测试它,你可以做以下事情:

错误

public function errorAction() 
{ 
    $log = $this->getServiceLocator()->get('log'); // init logger. You shouldn't use getServiceLocator() in controller. Recommended way is injecting through factory 
    array_merge([], 111); 
} 

应该在写日志:

2017-03-09T15:33:47+01:00 WARN (4): array_merge(): Argument #2 is not an array {"errno":2,"file":"[...]\\module\\Application\\src\\Application\\Controller\\IndexController.php","line":80} 

致命错误

public function fatalErrorAction() 
{ 
    $log = $this->getServiceLocator()->get('log'); // init logger. You shouldn't use getServiceLocator() in controller. Recommended way is injecting through factory 
    $class = new ClassWhichDoesNotExist(); 
} 

登录:

2017-03-09T15:43:06+01:00 ERR (3): Class 'Application\Controller\ClassWhichDoesNotExist' not found {"file":"[...]\\module\\Application\\src\\Application\\Controller\\IndexController.php","line":85} 

或者你如果您需要全局记录器,可以在Module.php文件中初始化记录器。

我不认为有可能在控制器的操作中记录异常。我是不确定,但操作是在try/catch块中调度的。

+0

我修正了这个问题,并添加了致命的错误记录。我仍然看不到日志中的th th声。我编辑了我的示例端点不起作用的问题。例外情况未处理。 –

+0

我编辑了我的答案..你只是没有初始化记录器。在配置文件中注册服务/模型,但Zend在第一次调用这个服务之后从它创建类。 – SzymonM

+0

谢谢。那样做了! –