{"action":"opened","issue":{"url":"https://api.github.com/repos/afrihost/BaseCommandBundle/issues/15","repository_url":"https://api.github.com/repos/afrihost/BaseCommandBundle","labels_url":"https://api.github.com/repos/afrihost/BaseCommandBundle/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/afrihost/BaseCommandBundle/issues/15/comments","events_url":"https://api.github.com/repos/afrihost/BaseCommandBundle/issues/15/events","html_url":"https://github.com/afrihost/BaseCommandBundle/issues/15","id":138698064,"number":15,"title":"Unhandled Exception Listener","user":{"login":"mostertb","id":2180195,"avatar_url":"https://avatars.githubusercontent.com/u/2180195?v=3","gravatar_id":"","url":"https://api.github.com/users/mostertb","html_url":"https://github.com/mostertb","followers_url":"https://api.github.com/users/mostertb/followers","following_url":"https://api.github.com/users/mostertb/following{/other_user}","gists_url":"https://api.github.com/users/mostertb/gists{/gist_id}","starred_url":"https://api.github.com/users/mostertb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mostertb/subscriptions","organizations_url":"https://api.github.com/users/mostertb/orgs","repos_url":"https://api.github.com/users/mostertb/repos","events_url":"https://api.github.com/users/mostertb/events{/privacy}","received_events_url":"https://api.github.com/users/mostertb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-05T17:04:14Z","updated_at":"2016-03-05T17:04:14Z","closed_at":null,"body":"**Problem:** When a command is being run in the background (such as via cron), there is no indication that the execution we terminated early due to an unhandled exception. Symfony outputs an error to the terminal however, in many cases this output is ignored in favour of log files when executing in such an automated background fashion.\r\n\r\n**Solution:** Add a kernel listener for the `console.exception` event that attempts to log the exception to a logfile if it was thrown by a command that extends `BaseComand` (using the command's own logger)\r\n\r\nWe already have something like this in the internal Afrihost codebase:\r\n```PHP\r\n public function onConsoleException(ConsoleExceptionEvent $event)\r\n    {\r\n        // If the command is a subclass of AHCommandBase, try log the exception to the command's log file\r\n        if($event->getCommand() instanceof AHCommandBase){\r\n            try{\r\n                /* @var $command AHCommandBase */\r\n                $command = $event->getCommand();\r\n                $exception = $event->getException();\r\n\r\n                $message = 'Uncaught Exception Encountered! Message: '.$exception->getMessage().PHP_EOL.\r\n                    'File: '.$exception->getFile().PHP_EOL.\r\n                    'Line: '.$exception->getLine().PHP_EOL;\r\n                $command->getLogger()->addEmergency($message);\r\n                $command->getLogger()->addInfo('Trace: '.$exception->getTraceAsString());\r\n            } catch(\\Exception $e){\r\n                // do nothing if logging throws further exception\r\n            }\r\n        }\r\n    }\r\n```\r\nThis just needs to be incorporated in to the BaseCommandBundle so that:\r\n- [ ]  The listener is an opt in feature (so that we are not forcing everyone's kernel to be clogged up with our listener)\r\n- [ ] The log level of the log entry can be defined in config\r\n- [ ] Something is done about making the log entry format more compatible. Not everyone likes to have erroneous newline characters in their log files (this breaks parsing)\r\n\r\nThe last task is probably a 'nice to have'"}}