How to send logs to email on exception or error in Symfony?

My requirements: Whenever an error occurs in application send that error logs to email.

I am trying to send any exception/error logs to email which occurs in our app. Here is what I tried but I am not receiving logs in email.

Note: Email sending functionality is working fine because I have tested it in a test controller. But it’s not working with this Listener.

What happens when I hit TestController Index Route?

When I vist index route it throws 500 internal server error ( because we intentionality thrown an exception ). However if this exception occurs it should send the logs to email. Because in the ExceptionListener I have coded the email sending of logs. But it is not sending logs (:

ExceptionListener.php:

<?php

namespace AppEventListener;

use AppServiceEmailService;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpKernelEventExceptionEvent;
use SymfonyComponentMailerExceptionTransportExceptionInterface;

class ExceptionListener
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public function onKernelException(EmailService $emailService, ExceptionEvent $event)
    {
        // You get the exception object from the received event
        $exception = $event->getThrowable();
        $message = sprintf(
            'My Error says: %s with code: %s',
            $exception->getMessage(),
            $exception->getCode()
        );

        try {
            $emailService->sendLogToMail("[email protected]", $message);
        } catch (TransportExceptionInterface $e) {
            //
        }

    }
}

I have registered the Listener in services.yaml:

AppEventListenerExceptionListener:
        tags:
            - { name: kernel.event_listener, event: kernel.exception }

TestController where I intentionality generate an exception:

<?php

namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use Exception;

class TestController extends AbstractController
{

    public function index() {

      throw new Exception("It's bad");

    }

}

I am using Symfony v4.4

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *