diff --git a/CHANGELOG.md b/CHANGELOG.md index bb668e6..f478c1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 swiftmailer extension Change Log 2.0.4 under development ----------------------- -- no changes in this release. +- Enh #4: Added ability to pass SwiftMailer log entries to `Yii::info()` (klimov-paul) 2.0.3 March 01, 2015 diff --git a/Logger.php b/Logger.php new file mode 100644 index 0000000..9d58f68 --- /dev/null +++ b/Logger.php @@ -0,0 +1,58 @@ + [ + * 'targets' => [ + * [ + * 'class' => 'yii\log\FileTarget', + * 'categories' => ['yii\swiftmailer\Logger::add'], + * ], + * ], + * ], + * ~~~ + * + * @author Paul Klimov + * @since 2.0 + */ +class Logger implements \Swift_Plugins_Logger +{ + /** + * @inheritdoc + */ + public function add($entry) + { + Yii::info($entry, __METHOD__); + } + + /** + * @inheritdoc + */ + public function clear() + { + // empty method stub + } + + /** + * @inheritdoc + */ + public function dump() + { + return ''; + } +} \ No newline at end of file diff --git a/Mailer.php b/Mailer.php index 179365c..538bc83 100644 --- a/Mailer.php +++ b/Mailer.php @@ -79,6 +79,12 @@ class Mailer extends BaseMailer * @var string message default class name. */ public $messageClass = 'yii\swiftmailer\Message'; + /** + * @var boolean whether to enable writing of the SwiftMailer internal logs using Yii log mechanism. + * If enabled [[Logger]] plugin will be attached to the [[transport]] for this purpose. + * @see Logger + */ + public $enableSwiftMailerLogging = false; /** * @var \Swift_Mailer Swift mailer instance. @@ -163,10 +169,24 @@ class Mailer extends BaseMailer if (isset($config['plugins'])) { $plugins = $config['plugins']; unset($config['plugins']); + } else { + $plugins = []; } + + if ($this->enableSwiftMailerLogging) { + $plugins[] = [ + 'class' => 'Swift_Plugins_LoggerPlugin', + 'constructArgs' => [ + [ + 'class' => 'yii\swiftmailer\Logger' + ] + ], + ]; + } + /* @var $transport \Swift_MailTransport */ $transport = $this->createSwiftObject($config); - if (isset($plugins)) { + if (!empty($plugins)) { foreach ($plugins as $plugin) { if (is_array($plugin) && isset($plugin['class'])) { $plugin = $this->createSwiftObject($plugin);