ZF2-520: Logger exception handler generates a badly formatted log message

Description

ZF2 RC7 Steps to reproduce: Initialize logging with writer and register the exception handler Logger::registerExceptionHandler Create a view script that throws an exception Dispatch the controller Note that request fails

Expected Log will hold a properly formatted exception message with readable trace, e.g.


ERR (3): exception 'Zend\Form\Exception\DomainException' with message 'Zend\Form\View\Helper\FormLabel::__invoke expects either label ..... Form/View/Helper/FormLabel.php:114
Stack trace:
#0 [internal function]: Zend\Form\View\Helper\FormLabel->__invoke(Object(Zend\Form\Element))
#1 /home/yonni/Zend/workspaces/DefaultWorkspace/ZendFramework-minimal-2.0.0rc7/library/Zend/View/Renderer/PhpRenderer.php(353): call_user_func_array(Object(Zend\Form\View\Helper\FormLabel), Array)
#2 /home/yonni/Zend/workspaces/DefaultWorkspace/gui.new/module/ZendServer/src/ZendServer/View/Helper/Form/Renderer/Table.php(36): Zend\View\Renderer\PhpRenderer->__call('FormLabel', Array)

Actual Log holds a json_encoded trace which is very difficult to decipher and seems to have gone through multiple character escaping. The entry is one long string, I broke it apart for easier reading


Zend\Form\View\Helper\FormLabel::__invoke expects ..... neither found {"file":"\/home\/yonni\/Zend\/workspaces\/DefaultWorkspace\/ZendFramework-minimal-2.0.0rc7\/library\/Zend\/Form\/View\/Helper\/FormLabel.php","line":114,"trace":"[\"
{\\\"function\\\":\\\"__invoke\\\",\\\"class\\\":\\\"Zend\\\\\\\\Form\\\\\\\\View\\\\\\\\Helper\\\\\\\\FormLabel\\\",\\\"type\\\":\\\"->\\\",\\\"args\\\":\\\"
[\\\\\\\"object(Zend\\\\\\\\\\\\\\\\Form\\\\\\\\\\\\\\\\Element) {}\\\\\\\"]\\\"}\",
\"{\\\"file\\\":\\\"\\\\\\\/home\\\\\\\/yonni\\\\\\\/Zend\\\\\\\/workspaces\\\\\\\/DefaultWorkspace\\\\\\\/ZendFramework-minimal-2.0.0rc7\\\\\\\/library\\\\\\\/Zend\\\\\\\/View\\\\\\\/Renderer\\\\\\\/PhpRenderer.php\\\",\\\"line\\\":353,\\\"function\\\":\\\"call_user_func_array\\\",\\\"args\\\":
\\\"[\\\\\\\"object(Zend\\\\\\\\\\\\\\\\Form\\\\\\\\\\\\\\\\View\\\\\\\\\\\\\\\\Helper\\\\\\\\\\\\\\\\FormLabel) {}\\\\\\\",
\\\\\\\"[\\\\\\\\\\\\\\\"object(Zend\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Form\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Element) {}\\\\\\\\\\\\\\\"]\\\\\\\"]\\\"}\",
\"{\\\"file\\\":\\\"\\\\\\\/home\\\\\\\/yonni\\\\\\\/Zend\\\\\\\/workspaces\\\\\\\/DefaultWorkspace\\\\\\\/gui.new\\\\\\\/module\\\\\\\/ZendServer\\\\\\\/src\\\\\\\/ZendServer\\\\\\\/View\\\\\\\/Helper\\\\\\\/Form\\\\\\\/Renderer\\\\\\\/Table.php\\\",\\\"line\\\":36,\\\"function\\\":\\\"__call\\\",\\\"class\\\":\\\"Zend\\\\\\\\View\\\\\\\\Renderer\\\\\\\\PhpRenderer\\\",\\\"type\\\":\\\"->\\\",\\\"args\\\":\\\"[\\\\\\\"FormLabel\\\\\\\",\\\\\\\"[\\\\\\\\\\\\\\\"object(Zend\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Form\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Element) {}\\\\\\\\\\\\\\\"]\\\\\\\"]\\\"}\",\"{\\\"file\\\":\\\"\\\\\\\/home\\\\\\\/yonni
.....

To resolve this locally I changed the following line (Zend\Log\Logger.php:437)


$logger->log(Logger::ERR, $exception->getMessage(), $extra);

To


$logger->log(Logger::ERR, $exception);

Effectively forcing the logger to use its own internal normalization and formatter

Comments

You can define your own exception handler. I don't think that we use the toString render of an exception, but improve the exception formatter.

I hope that you have extended your zf2 version instead of change the file. Do you know it's bad (practice)?

Too many slashes :)

Naturally, extended outside the framework. Just to be sure this is clear - the same thing happens when any sort of extras array is passed, not only exceptions

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2555