Browse Source

Added ability to pass SwiftMailer log entries to `Yii::info()`

tags/2.0.4
Klimov Paul 10 years ago
parent
commit
d30d730b5e
  1. 2
      CHANGELOG.md
  2. 58
      Logger.php
  3. 22
      Mailer.php

2
CHANGELOG.md

@ -4,7 +4,7 @@ Yii Framework 2 swiftmailer extension Change Log
2.0.4 under development 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 2.0.3 March 01, 2015

58
Logger.php

@ -0,0 +1,58 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\swiftmailer;
use Yii;
/**
* Logger is as SwiftMailer plugin, which allows passing of the SwiftMailer internal logs to the
* Yii logging mechanism. Each native SwiftMailer log message will be converted into Yii 'info' log entry.
*
* In order to catch logs written by this class, you need to setup a log route for 'yii\swiftmailer\Logger::add' category.
* For example:
*
* ~~~
* 'log' => [
* 'targets' => [
* [
* 'class' => 'yii\log\FileTarget',
* 'categories' => ['yii\swiftmailer\Logger::add'],
* ],
* ],
* ],
* ~~~
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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 '';
}
}

22
Mailer.php

@ -79,6 +79,12 @@ class Mailer extends BaseMailer
* @var string message default class name. * @var string message default class name.
*/ */
public $messageClass = 'yii\swiftmailer\Message'; 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. * @var \Swift_Mailer Swift mailer instance.
@ -163,10 +169,24 @@ class Mailer extends BaseMailer
if (isset($config['plugins'])) { if (isset($config['plugins'])) {
$plugins = $config['plugins']; $plugins = $config['plugins'];
unset($config['plugins']); unset($config['plugins']);
} else {
$plugins = [];
} }
if ($this->enableSwiftMailerLogging) {
$plugins[] = [
'class' => 'Swift_Plugins_LoggerPlugin',
'constructArgs' => [
[
'class' => 'yii\swiftmailer\Logger'
]
],
];
}
/* @var $transport \Swift_MailTransport */ /* @var $transport \Swift_MailTransport */
$transport = $this->createSwiftObject($config); $transport = $this->createSwiftObject($config);
if (isset($plugins)) { if (!empty($plugins)) {
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
if (is_array($plugin) && isset($plugin['class'])) { if (is_array($plugin) && isset($plugin['class'])) {
$plugin = $this->createSwiftObject($plugin); $plugin = $this->createSwiftObject($plugin);

Loading…
Cancel
Save