6

I'm trying to configure an email logging in Symfony. I followed the cookbook and it works but I have a problem with Fatal errors.

Those errors aren't logged in prod mode. I figured out that when I add Debug::enable(); to app.php, the error get logged, however I still don't get an email.

Here is the relevant configuration:

    mail:
        type:         fingers_crossed
        action_level: critical
        handler:      buffer
    buffer:
        type: buffer
        handler: swift
    swift:
        type:       swift_mailer
        from_email: %error_mail_from%
        to_email:   %error_mail_to%
        subject:    %error_mail_subject%
        level:      debug
Nemo64
  • 2,535
  • 3
  • 20
  • 24
  • You might change critical to error as that you will give you 400x messages as well as 500x messages. But @Yann Eugone is correct, a fatal error shuts everything down and it's really not practical to catch those. Of course if you are getting fatal errors in production then it implies that your testing methodology needs work. – Cerad Sep 23 '14 at 14:08
  • 1
    @Cerad 4xx are not a problem. They happen even if someone accesses the the page with an ipad (the tool isn't optimized for that) and it requests it's apple-touch-icon so i don't care about those. I also know that a fatal error should never happen in production but if it does... I want to know! – Nemo64 Sep 24 '14 at 08:31

3 Answers3

2

This is not an easy thing to log PHP Fatal Errors, because whenever the error is thrown, PHP shutdown... However, there is a function that can be used to do a little thing just before the process shut down : register_shutdown_function

Have a look to How do I catch a PHP Fatal Error

This is how Symfony's Debug::enable(); is doing the trick. Have a look to https://github.com/symfony/Debug/blob/master/ErrorHandler.php#L118

Community
  • 1
  • 1
Yann Eugoné
  • 1,311
  • 1
  • 8
  • 17
1

Which Symfony version are you using?

Seems like from 2.3 there's a nice improvement that allows you to do that (logging fatal errors). Have a look at this: https://github.com/symfony/symfony/pull/6474

Magd Kudama
  • 3,229
  • 2
  • 21
  • 25
  • I have multiple symfony instances. The oldest is 2.1. I'll look if it works in newer version. – Nemo64 Nov 06 '14 at 10:38
0

I had the same problem ( fatal erros logged in production but emails not sent) and I managed to make it work adding to my config_prod.php :

services:
  mydebug.debug_handlers_listener:
              class: Symfony\Component\HttpKernel\EventListener\DebugHandlersListener
              arguments:
                  - { 0:"@http_kernel", 1:'terminateWithException'}
              tags:
                   - { name: kernel.event_subscriber }

I found that such a service is defined in \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\debug.xml but not in debug_prod.xml.

With callback to terminateWithException it works fine in my application.

Krewetka
  • 99
  • 6