Several handlers work fine for me but the configuration for SwiftMailer presents an obstacle I've yet to solve. Local mail delivery works fine but when I want to define the mailer arguments I'm unable to configure it correctly.

monolog.handler.myhandler:
  class: \Monolog\Handler\SwiftMailerHandler
  arguments:
    mailer:
      class: Swift_Mailer
      transport:
        class: Swift_SmtpTransport
        host: '192.168.0.1'
        port: 25
      message: 'Example'
      level: DEBUG

I've tried various variants of this, such as absolute class names like \Swift_Mailer and or the arguments wrapped in array instead of as named attributes but the best I've received until now is mailer being null instead of Swift_Mailer. I think this is a bug in terms of how attributes can be defined here but am leaving it as support request for now, should this be a user error.

The only related issue I've found on this topic is https://github.com/theorchard/monolog-cascade/issues/59

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

grahl created an issue. See original summary.

lussoluca’s picture

Services in Service Container are not configurabile like bundles in a Symfony application.
I think the solution here could be create a new custom handler class, subclassing \Monolog\Handler\SwiftMailerHandler and in the new class __construct() call the parent one with all the needed arguments (maybe reading the arguments from the Configuration Management System).

grahl’s picture

Assigned: Unassigned » grahl
Status: Active » Needs work

Thanks for your feedback, that helps.

I'll provide an example in this issue when I have that running.

lussoluca’s picture

Status: Needs work » Needs review
Issue tags: +GlobalSprintWeekend2018
FileSize
1.67 KB

This patch adds a new DrupalMail handler to send a log message by mail.

lussoluca’s picture

FileSize
2.3 KB

With updated documentation

rp7’s picture

The provided example (with only modifying the error level) doesn't work with my me:

  monolog.handler.mail:
    class: Drupal\monolog\Monolog\Handler\DrupalMailHandler
    arguments: ['mail@example.com', 'monolog.level.error']

I did however get it to work like this:

  monolog.handler.mail:
    class: Drupal\monolog\Monolog\Handler\DrupalMailHandler
    arguments: ['hello@world.com', 400]

For some reason 'monolog.level.error' is not automatically converted the equivalent number. Is this only on my installation or are other people experiencing this as well?

grahl’s picture

Assigned: grahl » Unassigned

Sorry, never got around to helping in this issue and am not working with Monolog+Swiftmailer at the moment.

lussoluca’s picture

Could you please try with:

monolog.handler.mail:
    class: Drupal\monolog\Monolog\Handler\DrupalMailHandler
    arguments: ['mail@example.com', '%monolog.level.error%']

  • lussoluca committed dcbbf6c on 8.x-1.x
    Issue #2915422 by lussoluca: Swiftmailer configuration cannot be set
    
lussoluca’s picture

Status: Needs review » Fixed

Committed and pushed to 8.x-1.x

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.