rollbar / rollbar-php-symfony-bundle

Bundle for Symfony that integrates Rollbar tracker
MIT License
28 stars 25 forks source link

Uncaught Exception becomes two occurences in Rollbar app #36

Closed hngl closed 6 years ago

hngl commented 6 years ago

When using a basic Symfony 4 setup with the setup of the bundle as described in the Rollbar Symfony docs, one Exception thrown becomes 2 lines in the occurrences table.

trace.exception.message custom.channel custom.datetime custom.level custom.payload.body.trace_chain
Example msg app 1539001010 error [{"frames":...
Example msg request 1539001009 critical (empty)

Setup

config/bundles.php

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
   /* ... */

    // RollbarBundle must be included _after_ MonologBundle
    Rollbar\Symfony\RollbarBundle\RollbarBundle::class => ['prod' => true],
];

config/packages/prod/rollbar.yaml

rollbar:
  enabled: true
  access_token: '%env(ROLLBAR_TOKEN)%'
  environment: '%env(APP_HOST)%'

config/packages/monolog.yaml

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error # log all errors with status code >= 400
            handler:      grouped
        grouped:
            type:    group
            members: [streamed, deduplicated]
        streamed:
            type:  stream
            path:  '%kernel.logs_dir%/%kernel.environment%.log'
            level: error
        deduplicated:
            type:    deduplication
            handler: swift
        swift:
            type:       swift_mailer
            from_email: 'noreply@example.org'
            to_email:   '%env(SERVER_ADMIN)%'
            subject:    '%%message%%'
            level:      notice
            formatter:  monolog.formatter.html
            content_type: text/html

config/packages/prod/monolog.yaml

monolog:
  handlers:
    # This appends `rollbar` to the environment-independent list of handlers
    rollbar:
      type: service
      id: Rollbar\Monolog\Handler\RollbarHandler
hngl commented 6 years ago

After some more testing, I can make the following distinction.

Errors logged via a call to Monolog, are logged once.

 $psrLogger->error('Testing Rollbar', ['someVar' => 'someValue']);

Uncaught Exceptions are logged twice.

throw new TestingRollbarException('Is this thing on?');
hngl commented 6 years ago

The problem is caused by having the bundle register listeners for the kernel.exception event, in addition to Symfony listeners, which log to Monolog.

Symfony standard registers the Symfony\Component\HttpKernel\EventListener\ExceptionListener::logKernelException() listener. This listener sends the exception to the default logger . That is usually Monolog.

Output of bin/console debug:event-dispatcher kernel.exception:

"kernel.exception" event
------------------------

 ------- ------------------------------------------------------------------------------------ ----------
  Order   Callable                                                                             Priority
 ------- ------------------------------------------------------------------------------------ ----------
  #1      App\Api\EventSubscriber\ApiExceptionSubscriber::handleApiException()                 2049
  #2      App\EventSubscriber\AccountExceptionSubscriber::handleUnknownAccountException()      3
  #3      App\EventSubscriber\AccountExceptionSubscriber::handleExpiredAccountException()      2
  #4      Rollbar\Symfony\RollbarBundle\EventListener\ErrorListener::onKernelException()       0
  #5      Symfony\Component\HttpKernel\EventListener\ExceptionListener::logKernelException()   0
  #6      Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onException()    0
  #7      Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelException()       -64
  #8      Rollbar\Symfony\RollbarBundle\EventListener\ExceptionListener::onKernelException()   -100
  #9      Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException()    -128
 ------- ------------------------------------------------------------------------------------ ----------

That is how when using this SDK, uncaught exceptions show up twice in Rollbar.

@ArturMoczulski Could you please remove the listeners?

ArturMoczulski commented 6 years ago

@hngl thank you for PR. I merged it in. phpunit exiting with a non-zero status will be resolved as a separate issue since I identified the problem happening in the master branch as well.