2017-03-08 44 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

謝謝。那樣做了! –