Last active
December 11, 2017 11:48
-
-
Save webdevilopers/364549d31aa12a833e4eab0ae4a9a36e to your computer and use it in GitHub Desktop.
Configure Symfony Monolog to send error emails when exceptions are thrown in console commands
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
monolog: | |
handlers: | |
main: | |
type: fingers_crossed | |
action_level: error | |
handler: grouped | |
grouped: | |
type: group | |
members: [streamed, console, buffered] | |
streamed: | |
type: stream | |
path: "%kernel.logs_dir%/%kernel.environment%.log" | |
level: debug | |
console: | |
type: console | |
buffered: | |
type: buffer | |
handler: swift | |
swift: | |
type: swift_mailer | |
from_email: [email protected] | |
to_email: [email protected] | |
subject: An Error Occurred! | |
level: debug | |
swiftmailer: | |
#port: 587 | |
#encryption: tls | |
delivery_address: '[email protected]' |
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
<?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="Acme\Foo\Infrastructure\Symfony\AppBundle\Listener\ConsoleExceptionListener" public="true"> | |
<argument type="service" id="logger" on-invalid="null" /> | |
<tag name="kernel.event_subscriber" /> | |
<tag name="monolog.logger" channel="console" /> | |
</service> | |
</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
<?php | |
namespace Acme\Foo\Infrastructure\Symfony\AppBundle\Listener; | |
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 <[email protected]> | |
* @author Robin Chalas <[email protected]> | |
*/ | |
class ConsoleExceptionListener 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; | |
} | |
} |
Any exception caused via UI is now logged to file and emailed. But not if the exception is thrown when executing a console command.
Looks like @symfony 3.3 solved this:
http://symfony.com/blog/new-in-symfony-3-3-automatic-console-logging
Thanks @chalasr and @jaitsu87!
See added code for integrating the following patch into @symfony 2.8:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The dev.log logs the following when I cause an exception via console: