-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from kronostechnologies/dev/add-logger-decorator
Ajouter de LoggerDecorator pour filtrer les messages au niveau du Logger
- Loading branch information
Showing
5 changed files
with
262 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Kronos\Log; | ||
|
||
use Kronos\Log\Exception\InvalidLogLevel; | ||
use Psr\Log\LogLevel; | ||
|
||
class LogLevelHelper | ||
{ | ||
public const levelPriorities = [ | ||
LogLevel::EMERGENCY => 7, | ||
LogLevel::ALERT => 6, | ||
LogLevel::CRITICAL => 5, | ||
LogLevel::ERROR => 4, | ||
LogLevel::WARNING => 3, | ||
LogLevel::NOTICE => 2, | ||
LogLevel::INFO => 1, | ||
LogLevel::DEBUG => 0 | ||
]; | ||
|
||
public static function isLower(string $baseLevel, string $toCompare): bool | ||
{ | ||
return self::levelPriorities[$toCompare] < self::levelPriorities[$baseLevel]; | ||
} | ||
|
||
public static function isHigher(string $baseLevel, string $toCompare): bool | ||
{ | ||
return self::levelPriorities[$toCompare] > self::levelPriorities[$baseLevel]; | ||
} | ||
|
||
/** | ||
* @param string $level | ||
* @throws InvalidLogLevel | ||
*/ | ||
public static function validateLogLevel(string $level): void | ||
{ | ||
if (!isset(self::levelPriorities[$level])) { | ||
throw new InvalidLogLevel($level); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Kronos\Log; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class LoggerDecorator implements LoggerInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $level = LogLevel::DEBUG; | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $delegate; | ||
|
||
public function __construct( | ||
LoggerInterface $delegate | ||
) { | ||
$this->delegate = $delegate; | ||
} | ||
|
||
public function setLevel(string $level) | ||
{ | ||
$this->level = $level; | ||
} | ||
|
||
public function emergency($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::EMERGENCY, $message, $context); | ||
} | ||
|
||
public function alert($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::ALERT, $message, $context); | ||
} | ||
|
||
public function critical($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::CRITICAL, $message, $context); | ||
} | ||
|
||
public function error($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::ERROR, $message, $context); | ||
} | ||
|
||
public function warning($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::WARNING, $message, $context); | ||
} | ||
|
||
public function notice($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::NOTICE, $message, $context); | ||
} | ||
|
||
public function info($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::INFO, $message, $context); | ||
} | ||
|
||
public function debug($message, array $context = array()) | ||
{ | ||
$this->log(LogLevel::DEBUG, $message, $context); | ||
} | ||
|
||
public function log($level, $message, array $context = array()) | ||
{ | ||
if (!LogLevelHelper::isLower($this->level, (string)$level)) { | ||
$this->delegate->log($level, $message, $context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
use Kronos\Log\Exception\InvalidLogLevel; | ||
use Kronos\Log\LogLevelHelper; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LogLevel; | ||
|
||
class LogLevelHelperTest extends TestCase | ||
{ | ||
public function test_isLower_shouldReturnFalseWhenLevelIsEqual(): void | ||
{ | ||
$isLower = LogLevelHelper::isLower(LogLevel::DEBUG, LogLevel::DEBUG); | ||
|
||
self::assertFalse($isLower); | ||
} | ||
|
||
/** | ||
* @dataProvider provideLevels | ||
*/ | ||
public function test_isLower_shouldReturnFalseWhenLevelIsHigher($baseLevel, $toCompare): void | ||
{ | ||
$isLower = LogLevelHelper::isLower($baseLevel, $toCompare); | ||
|
||
self::assertFalse($isLower); | ||
} | ||
|
||
public function test_validateLogLevel_shouldThrowInvalidLogLevelWhenLevelIsUnknown(): void | ||
{ | ||
self::expectException(InvalidLogLevel::class); | ||
|
||
LogLevelHelper::validateLogLevel('unknown level'); | ||
} | ||
|
||
public function provideLevels(): array | ||
{ | ||
return [ | ||
[LogLevel::ALERT, LogLevel::EMERGENCY], | ||
[LogLevel::CRITICAL, LogLevel::ALERT], | ||
[LogLevel::ERROR, LogLevel::CRITICAL], | ||
[LogLevel::WARNING, LogLevel::ERROR], | ||
[LogLevel::NOTICE, LogLevel::WARNING], | ||
[LogLevel::INFO, LogLevel::NOTICE], | ||
[LogLevel::DEBUG, LogLevel::INFO], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
use Kronos\Log\LoggerDecorator; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class LoggerDecoratorTest extends TestCase | ||
{ | ||
/** | ||
* @var LoggerInterface & MockObject | ||
*/ | ||
private $delegate; | ||
/** | ||
* @var LoggerDecorator | ||
*/ | ||
private $loggerDecorator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->delegate = $this->createMock(LoggerInterface::class); | ||
$this->loggerDecorator = new LoggerDecorator($this->delegate); | ||
} | ||
|
||
public function test_shouldLogWhenMessageLevelIsHigherThanLogger(): void | ||
{ | ||
$this->loggerDecorator->setLevel(LogLevel::INFO); | ||
$this->delegate | ||
->expects($this->once()) | ||
->method('log') | ||
->with(LogLevel::WARNING, 'a message'); | ||
|
||
$this->loggerDecorator->log(LogLevel::WARNING, 'a message'); | ||
} | ||
|
||
/** | ||
* @dataProvider provideLowerLogLevels | ||
*/ | ||
public function test_shouldNotLogWhenLoggerLevelIsHigherThanMessage($loggerLevel, $levelOfMessage): void | ||
{ | ||
$this->loggerDecorator->setLevel($loggerLevel); | ||
$this->delegate->expects($this->never())->method('log'); | ||
|
||
$this->loggerDecorator->log($levelOfMessage, 'a message'); | ||
} | ||
|
||
public function provideLowerLogLevels(): array | ||
{ | ||
return [ | ||
[LogLevel::INFO, LogLevel::DEBUG], | ||
[LogLevel::NOTICE, LogLevel::INFO], | ||
[LogLevel::WARNING, LogLevel::NOTICE], | ||
[LogLevel::ERROR, LogLevel::WARNING], | ||
[LogLevel::CRITICAL, LogLevel::ERROR], | ||
[LogLevel::EMERGENCY, LogLevel::CRITICAL] | ||
]; | ||
} | ||
} |