@
@
@deffect2014-05-16 12:33:20
Zend Framework
@deffect, 2014-05-16 12:33:20

ZF2. Why doesn't Zend\Log\Logger::registerErrorHandler work?

Hello.
In a Zend Framework 2 project, I make my own log:

public function indexAction()
    {     
        $writer = new \Zend\Log\Writer\Stream($_SERVER['DOCUMENT_ROOT'].'/logs/log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        \Zend\Log\Logger::registerErrorHandler($logger); 
        $logger->info('Тест ошибки');
        // тут намеренно делаем ошибку, чтобы увидеть её в логах
        return;
    }

In the logs I see:
2014-05-16T12:40:58+04:00 INFO (6): Error test
There is no error message itself!
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2014-06-09
@Keanor

You set the text:
And you get it in the log:
what other text do you want to get in it?
Framework exceptions, and a particular type of error is not something you can catch.
We are using the following code

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $this->attachEventListeners($eventManager);

        $eventManager->attach('dispatch.error', function ($event) use ($e) {
            $sm = $e->getApplication()->getServiceManager();
            $service = $sm->get('Application\Service\ErrorHandling');

            $exception = $event->getResult()->exception;
            if ($exception)
                $service->logException($exception);
        });

        set_error_handler(function($c, $m, $f, $l) use ($e) {
            $sm = $e->getApplication()->getServiceManager();
            $service = $sm->get('Application\Service\ErrorHandling');
            $service->logException(func_get_args());
        });

        register_shutdown_function(function() use ($e) {
            $error = error_get_last();
            $sm = $e->getApplication()->getServiceManager();

            if ($error && ($error['type'] == E_ERROR || $error['type'] == E_PARSE || $error['type'] == E_COMPILE_ERROR))
            {
                $service = $sm->get('Application\Service\ErrorHandling');
                if (strpos($error['message'], 'Allowed memory size') == 0)
                {
                    ini_set('memory_limit', (intval(ini_get('memory_limit'))+64)."M");
                }
                $service->logException($error);
            }
        });
    }

    ....
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question