/** * Handle php errors and Zf2 Mvc errors * * @param MvcEvent $e */ public function onBootstrap(MvcEvent $e) { $eventManager = $e->getApplication()->getEventManager(); /** @var ModuleOptions $moduleOptions */ $moduleOptions = $e->getApplication()->getServiceManager()->get('Zf2LoggerOptions'); /** @var LoggerService $loggerService */ $loggerService = $e->getApplication()->getServiceManager()->get('Zf2LoggerService'); if ($moduleOptions->isLogMvcErrors()) { // Handle zf2 dispatch error $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$loggerService, 'errorEventHandler']); // Handle zf2 render error $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, [$loggerService, 'errorEventHandler']); } if ($moduleOptions->isLogPhpErrors()) { Logger::registerExceptionHandler($loggerService->getLogger()); set_error_handler(function ($severity, $message, $file, $line) { if (!(error_reporting() & $severity)) { return; } throw new \ErrorException($message, 0, $severity, $file, $line); }); } if ($moduleOptions->isRegisterShutdownFunction()) { Logger::registerFatalErrorShutdownFunction($loggerService->getLogger()); } }
/** * Registers the handlers for errors and exceptions. * * @param string $logFile * @param string $logDir */ public static function registerHandlers($logFile = 'error.log', $logDir = 'data/logs', $continue = true) { $logger = AbstractLogger::generateFileLogger($logFile, $logDir); Logger::registerErrorHandler($logger->getLogger(), $continue); Logger::registerFatalErrorShutdownFunction($logger->getLogger()); }
/** * @runInSeparateProcess */ public function testRegisterFatalShutdownFunction() { $writer = new MockWriter(); $this->logger->addWriter($writer); $result = Logger::registerFatalErrorShutdownFunction($this->logger); $this->assertTrue($result); // check for single error handler instance $this->assertFalse(Logger::registerFatalErrorShutdownFunction($this->logger)); $self = $this; register_shutdown_function(function () use($writer, $self) { $self->assertEquals($writer->events[0]['message'], 'Call to undefined method ZendTest\\Log\\LoggerTest::callToNonExistingMethod()'); }); // Temporary hide errors, because we don't want the fatal error to fail the test @$this->callToNonExistingMethod(); }