-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console][FrameworkBundle] Log console exceptions #21003
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
Merged
fabpot
merged 2 commits into
symfony:master
from
chalasr:automatic-console-exception-logging
Jan 23, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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" public="false"> | ||
<argument type="service" id="logger" on-invalid="null" /> | ||
<tag name="kernel.event_subscriber" /> | ||
<tag name="monolog.logger" channel="console" /> | ||
</service> | ||
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 think it could be useful to add a channel. for example 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. It makes sense, |
||
|
||
</services> | ||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Symfony/Component/Console/EventListener/ExceptionListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\EventListener; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Event\ConsoleEvent; | ||
use Symfony\Component\Console\ConsoleEvents; | ||
use Symfony\Component\Console\Event\ConsoleExceptionEvent; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @author James Halsall <james.t.halsall@googlemail.com> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class ExceptionListener implements EventSubscriberInterface | ||
{ | ||
private $logger; | ||
|
||
public function __construct(LoggerInterface $logger = null) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function onConsoleException(ConsoleExceptionEvent $event) | ||
{ | ||
if (null === $this->logger) { | ||
return; | ||
} | ||
|
||
$exception = $event->getException(); | ||
|
||
$this->logger->error('Exception thrown while running command "{command}". Message: "{message}"', array('exception' => $exception, 'command' => $this->getInputString($event), 'message' => $exception->getMessage())); | ||
} | ||
|
||
public function onConsoleTerminate(ConsoleTerminateEvent $event) | ||
{ | ||
if (null === $this->logger) { | ||
return; | ||
} | ||
|
||
$exitCode = $event->getExitCode(); | ||
|
||
if (0 === $exitCode) { | ||
return; | ||
} | ||
|
||
$this->logger->error('Command "{command}" exited with code "{code}"', array('command' => $this->getInputString($event), 'code' => $exitCode)); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
ConsoleEvents::EXCEPTION => array('onConsoleException', -128), | ||
ConsoleEvents::TERMINATE => array('onConsoleTerminate', -128), | ||
); | ||
} | ||
|
||
private static function getInputString(ConsoleEvent $event) | ||
{ | ||
$commandName = $event->getCommand()->getName(); | ||
$input = $event->getInput(); | ||
|
||
if (method_exists($input, '__toString')) { | ||
return str_replace(array("'$commandName'", "\"$commandName\""), $commandName, (string) $input); | ||
} | ||
|
||
return $commandName; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/Symfony/Component/Console/Tests/EventListener/ExceptionListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\EventListener; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Event\ConsoleExceptionEvent; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\Console\EventListener\ExceptionListener; | ||
use Symfony\Component\Console\Input\ArgvInput; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ExceptionListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testOnConsoleException() | ||
{ | ||
$exception = new \RuntimeException('An error occurred'); | ||
|
||
$logger = $this->getLogger(); | ||
$logger | ||
->expects($this->once()) | ||
->method('error') | ||
->with('Exception thrown while running command "{command}". Message: "{message}"', array('exception' => $exception, 'command' => 'test:run --foo=baz buzz', 'message' => 'An error occurred')) | ||
; | ||
|
||
$listener = new ExceptionListener($logger); | ||
$listener->onConsoleException($this->getConsoleExceptionEvent($exception, new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), 1)); | ||
} | ||
|
||
public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() | ||
{ | ||
$logger = $this->getLogger(); | ||
$logger | ||
->expects($this->once()) | ||
->method('error') | ||
->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run', 'code' => 255)) | ||
; | ||
|
||
$listener = new ExceptionListener($logger); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 255)); | ||
} | ||
|
||
public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() | ||
{ | ||
$logger = $this->getLogger(); | ||
$logger | ||
->expects($this->never()) | ||
->method('error') | ||
; | ||
|
||
$listener = new ExceptionListener($logger); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 0)); | ||
} | ||
|
||
public function testGetSubscribedEvents() | ||
{ | ||
$this->assertEquals( | ||
array( | ||
'console.exception' => array('onConsoleException', -128), | ||
'console.terminate' => array('onConsoleTerminate', -128), | ||
), | ||
ExceptionListener::getSubscribedEvents() | ||
); | ||
} | ||
|
||
public function testAllKindsOfInputCanBeLogged() | ||
{ | ||
$logger = $this->getLogger(); | ||
$logger | ||
->expects($this->exactly(3)) | ||
->method('error') | ||
->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run --foo=bar', 'code' => 255)) | ||
; | ||
|
||
$listener = new ExceptionListener($logger); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run', '--foo=bar')), 255)); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArrayInput(array('name' => 'test:run', '--foo' => 'bar')), 255)); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent(new StringInput('test:run --foo=bar'), 255)); | ||
} | ||
|
||
public function testCommandNameIsDisplayedForNonStringableInput() | ||
{ | ||
$logger = $this->getLogger(); | ||
$logger | ||
->expects($this->once()) | ||
->method('error') | ||
->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run', 'code' => 255)) | ||
; | ||
|
||
$listener = new ExceptionListener($logger); | ||
$listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->getMockBuilder(InputInterface::class)->getMock(), 255)); | ||
} | ||
|
||
private function getLogger() | ||
{ | ||
return $this->getMockForAbstractClass(LoggerInterface::class); | ||
} | ||
|
||
private function getConsoleExceptionEvent(\Exception $exception, InputInterface $input, $exitCode) | ||
{ | ||
return new ConsoleExceptionEvent(new Command('test:run'), $input, $this->getOutput(), $exception, $exitCode); | ||
} | ||
|
||
private function getConsoleTerminateEvent(InputInterface $input, $exitCode) | ||
{ | ||
return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->getOutput(), $exitCode); | ||
} | ||
|
||
private function getOutput() | ||
{ | ||
return $this->getMockBuilder(OutputInterface::class)->getMock(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You should add a
FileExistenceResource
resource so that if the console component is installed, the cache is automatically rebuilt.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.
Sorry but I don't understand. We are checking a class existence here,
FileExistenceResource
is about the filesystem, right?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.
Got it! You mean
ClassExistenceResource
:) done