Symfony2 logging 404 errors

前端 未结 1 1971
-上瘾入骨i
-上瘾入骨i 2021-01-01 07:44

I need to be able to log/receive an email when a 404 error occurs. I can see in the docs how to set up a new template for these errors, but how do I catch them in the first

相关标签:
1条回答
  • 2021-01-01 08:37

    Maybe adding an event listener listening for the kernel.exception event would do it? Check out http://symfony.com/doc/current/book/internals.html#kernel-exception-event along with http://symfony.com/doc/current/reference/dic_tags.html#dic-tags-kernel-event-listener

    A little example:

    1) Create a custom Listener

    //bundles/Acme/AcmeBundle/Listener/CustomListener.php
    
    namespace Acme\AcmeBundle\Listener;
    use Symfony\Component\EventDispatcher\Event;
    
    public class CustomListener {
        public function onKernelException(Event $event) {
            //Get hold of the exception
            $exception = $event->getException();
            //Do the logging
            // ...
        }
    }
    

    2) Add the listener to your config

    //config.yml
    services:
        kernel.listener.your_listener_name:
            class: Acme\AcmeBundle\Listener\CustomListener
            tags:
                - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
    

    To get hold of the logging or mailing (Swiftmailer) services, you might consider injecting them into the listener (http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services)

    0 讨论(0)
提交回复
热议问题