Last active
April 16, 2020 12:27
-
-
Save FabianSchmick/a92faa045f764e1a252182e795dd5899 to your computer and use it in GitHub Desktop.
Symfony - error reporting via email on critical errors or exceptions.
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: [rotating, console, deduplicated] | |
rotating: | |
type: rotating_file | |
level: debug | |
max_files: 30 | |
console: | |
type: console | |
level: debug | |
# process_psr_3_messages: false | |
deduplicated: | |
type: deduplication | |
handler: swift | |
swift: | |
type: swift_mailer | |
from_email: '[email protected]' | |
to_email: ['[email protected]'] | |
subject: 'Example.com: An Error Occurred! %%message%%' | |
level: critical | |
formatter: monolog.formatter.html | |
content_type: text/html |
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 AppBundle\EventListener; | |
use Symfony\Component\Console\Event\ConsoleErrorEvent; | |
use Psr\Log\LoggerInterface; | |
class ConsoleExceptionListener | |
{ | |
private $logger; | |
public function __construct(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function onConsoleError(ConsoleErrorEvent $event) | |
{ | |
$command = $event->getCommand(); | |
$error = $event->getError(); | |
$message = sprintf( | |
'%s: %s (uncaught error) at %s line %s while running console command `%s`', | |
get_class($error), | |
$error->getMessage(), | |
$error->getFile(), | |
$error->getLine(), | |
$command->getName() | |
); | |
$this->logger->critical($message, array('error' => $error)); | |
} | |
} |
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
services: | |
AppBundle\EventListener\ConsoleExceptionListener: | |
tags: | |
- { name: kernel.event_listener, event: console.error } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment