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 #21 from chadicus/master
Add InMemoryLogger
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace SubjectivePHP\Psr\Log; | ||
|
||
use ArrayObject; | ||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* PSR-3 Logger implementation writing to an array. | ||
*/ | ||
final class InMemoryLogger extends AbstractLogger implements LoggerInterface | ||
{ | ||
use MessageInterpolationTrait; | ||
|
||
/** | ||
* @var ArrayObject | ||
*/ | ||
private $logs; | ||
|
||
/** | ||
* Construct a new instance of the logger. | ||
* | ||
* @param ArrayObject $logs Container for log entries. | ||
*/ | ||
public function __construct(ArrayObject $logs) | ||
{ | ||
$this->logs = $logs; | ||
} | ||
|
||
/** | ||
* Logs with an arbitrary level. | ||
* | ||
* @param string $level A valid RFC-5424 log level. | ||
* @param string $message The base log message. | ||
* @param array $context Any extraneous information that does not fit well in a string. | ||
* | ||
* @return void | ||
*/ | ||
public function log($level, $message, array $context = []) | ||
{ | ||
$this->logs[] = [ | ||
'level' => $level, | ||
'message' => $this->interpolateMessage($message, $context), | ||
'context' => $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,42 @@ | ||
<?php | ||
|
||
namespace SubjectivePHPTest\Psr\Log; | ||
|
||
use ArrayObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LogLevel; | ||
use SubjectivePHP\Psr\Log\InMemoryLogger; | ||
|
||
/** | ||
* @coversDefaultClass \SubjectivePHP\Psr\Log\InMemoryLogger | ||
* @covers ::__construct | ||
*/ | ||
final class InMeoryLoggerTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @covers ::log | ||
* | ||
* @return void | ||
*/ | ||
public function logAlertWithMessageInterpolation() | ||
{ | ||
$exception = new \RuntimeException(); | ||
$logs = new ArrayObject(); | ||
$logger = new InMemoryLogger($logs); | ||
$logger->alert('logged {count} entries', ['exception' => $exception, 'count' => 1]); | ||
$this->assertSame( | ||
[ | ||
[ | ||
'level' => LogLevel::ALERT, | ||
'message' => 'logged 1 entries', | ||
'context' => [ | ||
'exception' => $exception, | ||
'count' => 1, | ||
], | ||
], | ||
], | ||
$logs->getArrayCopy() | ||
); | ||
} | ||
} |