-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] [Console] Add basic support for automatic console exception logging #19382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4284161
17ef8db
3d6b22b
0961f8c
5bd799e
80c3ac3
f7a7048
c21d206
2874431
bcbc8cb
da278e6
44a0822
5dc8b61
b196557
e6c4994
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="console.exception_listener" class="Symfony\Component\Console\EventListener\ExceptionListener"> | ||
<tag name="kernel.event_subscriber" /> | ||
<argument type="service" id="logger" on-invalid="null" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\EventListener; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\ConsoleEvents; | ||
use Symfony\Component\Console\Event\ConsoleExceptionEvent; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Console exception listener. | ||
* | ||
* Attempts to log exceptions or abnormal terminations of console commands. | ||
* | ||
* @package Symfony\Component\Console\EventListener; | ||
* @author James Halsall <james.t.halsall@googlemail.com> | ||
*/ | ||
class ExceptionListener implements EventSubscriberInterface | ||
{ | ||
protected $logger; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param LoggerInterface $logger A logger | ||
*/ | ||
public function __construct(LoggerInterface $logger = null) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Handles console command exception. | ||
* | ||
* @param ConsoleExceptionEvent $event Console event | ||
*/ | ||
public function onKernelException(ConsoleExceptionEvent $event) | ||
{ | ||
if (null === $this->logger) { | ||
return; | ||
} | ||
|
||
$exception = $event->getException(); | ||
|
||
$message = sprintf( | ||
'%s: %s (uncaught exception) at %s line %s while running console command `%s`', | ||
get_class($exception), | ||
$exception->getMessage(), | ||
$exception->getFile(), | ||
$exception->getLine(), | ||
$event->getCommand()->getName() | ||
); | ||
|
||
$this->logger->error($message, array('exception' => $exception)); | ||
} | ||
|
||
/** | ||
* Handles termination of console command. | ||
* | ||
* @param ConsoleTerminateEvent $event Console event | ||
*/ | ||
public function onKernelTerminate(ConsoleTerminateEvent $event) | ||
{ | ||
if (null === $this->logger) { | ||
return; | ||
} | ||
|
||
$exitCode = $event->getExitCode(); | ||
|
||
if ($exitCode === 0) { | ||
return; | ||
} | ||
|
||
if ($exitCode > 255) { | ||
$event->setExitCode(255); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, this should not be done here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great point, I will remove this |
||
} | ||
|
||
$message = sprintf('Command `%s` exited with status code %d'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks something else than what the title of this PR suggests. moreover, I'm not sure it's forth logging a non zero status code: it's the command's responsibility to know if it's worth logging of not, don't you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea of this PR is to add automatic exception logging "out of the box" with zero configuration. Maybe I'm misunderstanding but surely anything other than a 0 exit code would result in some kind of log entry, unless there's a fatal PHP error before Symfony kernel bootstraps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
|
||
$this->logger->warning($message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put |
||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
1E0A return array( | ||
ConsoleEvents::EXCEPTION => array('onKernelException', -128), | ||
ConsoleEvents::TERMINATE => array('onKernelTerminate', -128) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a much simpler:
$this->logger->error($exception->getMessage(), array('exception' => $exception));
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍