Browse Source

`yii\swiftmailer\Logger` now chooses logging level depending on incoming entry format

tags/2.0.7
Klimov Paul 8 years ago
parent
commit
cd5b231d0a
  1. 2
      CHANGELOG.md
  2. 20
      Logger.php
  3. 70
      tests/LoggerTest.php
  4. 5
      tests/MailerTest.php

2
CHANGELOG.md

@ -4,7 +4,7 @@ Yii Framework 2 swiftmailer extension Change Log
2.0.7 under development 2.0.7 under development
----------------------- -----------------------
- no changes in this release. - Enh #37: `yii\swiftmailer\Logger` now chooses logging level depending on incoming entry format (klimov-paul)
2.0.6 September 09, 2016 2.0.6 September 09, 2016

20
Logger.php

@ -37,7 +37,25 @@ class Logger implements \Swift_Plugins_Logger
*/ */
public function add($entry) public function add($entry)
{ {
Yii::info($entry, __METHOD__); $categoryPrefix = substr($entry, 0, 2);
switch ($categoryPrefix) {
case '++':
$level = \yii\log\Logger::LEVEL_TRACE;
break;
case '>>':
case '<<':
$level = \yii\log\Logger::LEVEL_INFO;
break;
case '!!':
$level = \yii\log\Logger::LEVEL_WARNING;
break;
}
if (!isset($level)) {
$level = \yii\log\Logger::LEVEL_INFO;
}
Yii::getLogger()->log($entry, $level, __METHOD__);
} }
/** /**

70
tests/LoggerTest.php

@ -0,0 +1,70 @@
<?php
namespace yiiunit\extensions\swiftmailer;
use Yii;
use yii\swiftmailer\Logger;
class LoggerTest extends TestCase
{
protected function getLastLogMessage()
{
return end(Yii::getLogger()->messages);
}
/**
* Data provider for [[testAdd()]]
* @return array test data
*/
public function dataProviderAdd()
{
return [
[
'>> command sent',
[
'message' => '>> command sent',
'level' => \yii\log\Logger::LEVEL_INFO,
]
],
[
'<< response received',
[
'message' => '<< response received',
'level' => \yii\log\Logger::LEVEL_INFO,
]
],
[
'++ transport started',
[
'message' => '++ transport started',
'level' => \yii\log\Logger::LEVEL_TRACE,
]
],
[
'!! error message',
[
'message' => '!! error message',
'level' => \yii\log\Logger::LEVEL_WARNING,
]
],
];
}
/**
* @dataProvider dataProviderAdd
*
* @param string $entry
* @param array $expectedLogMessage
*/
public function testAdd($entry, array $expectedLogMessage)
{
$logger = new Logger();
$logger->add($entry);
$logMessage = $this->getLastLogMessage();
$this->assertEquals($expectedLogMessage['message'], $logMessage[0]);
$this->assertEquals($expectedLogMessage['level'], $logMessage[1]);
}
}

5
tests/MailerTest.php

@ -7,11 +7,6 @@ use yii\swiftmailer\Mailer;
Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer'); Yii::setAlias('@yii/swiftmailer', __DIR__ . '/../../../../extensions/swiftmailer');
/**
* @group vendor
* @group mail
* @group swiftmailer
*/
class MailerTest extends TestCase class MailerTest extends TestCase
{ {
public function setUp() public function setUp()

Loading…
Cancel
Save