Monolog: Send error logs by mail causes an extra mail for every error sent

1.3k Views Asked by At

For every error log sent by mail we get another extra email with 2 lines of smtp debug log messages.

  • Newly created symfony demo (symfony new --demo)
  • Configured monolog to send error messages by mail as in the docs
  • symfony/monolog-bundle 3.7.0
  • Symfony Versions 5.2.1 / 5.2.9 / 5.3.0-RC1,
  • PHP 7.4.13
# config/packages/prod/monolog.yaml
monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: critical
            handler:      deduplicated
        deduplicated:
            type:    deduplication
            handler: symfony_mailer
        symfony_mailer:
            type:       symfony_mailer
            from_email: '[email protected]'
            to_email:   '[email protected]'
            subject:    'An Error Occurred! %%message%%'
            level:      debug

Monolog sends error messages as expected, but every mail is followed by a second one with this content:

[2021-05-26T10:49:47.683298+02:00] app.DEBUG: Email transport "Symfony\Component\Mailer\Transport\Smtp\SmtpTransport" stopping [] []
[2021-05-26T10:49:47.722980+02:00] app.DEBUG: Email transport "Symfony\Component\Mailer\Transport\Smtp\SmtpTransport" stopped [] []
  • no extra mail in dev or test environments
  • no extra mail when setting the framework config test: true in config/framework.yaml

Any ideas how to get rid of this extra email in production mode?

PS: There's also an open issue in the Symfony MonologBundle.

1

There are 1 best solutions below

0
On

As mentioned in the linked GitHub issue by @raziel057, a workaround would be to exclude the "mailer" channel.
https://github.com/symfony/monolog-bundle/issues/405#issuecomment-1069040234

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: critical
            handler:      grouped
            channels:     ["!mailer"]
        grouped:
            type:    group
            members: [streamed, deduplicated]
        streamed:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        deduplicated:
            type:    deduplication
            handler: symfony_mailer
        symfony_mailer:
            type:       symfony_mailer
            from_email: "%mailer_app_sender%"
            to_email:   "%mailer_team_receiver%"
            subject:    "An Error Occurred!"
            level:      debug