Last active
October 20, 2021 14:44
-
-
Save chrisguitarguy/04dcfc1622dc8d650a396a79572e293e to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Chrisguitarguy\Example; | |
use OutOfBoundsException; | |
use Countable; | |
use IteratorAggregate; | |
use Psr\Log\AbstractLogger; | |
class CollectingLogger extends AbstractLogger implements Countable, IteratorAggregate | |
{ | |
private array $messages = []; | |
public function log($level, $message, array $context=[]) : void | |
{ | |
$this->messages[] = [ | |
'level' => $level, | |
'message' => $message, | |
'context' => $context, | |
]; | |
} | |
public function getIterator() : iterable | |
{ | |
yield from $this->messages; | |
} | |
public function count() : int | |
{ | |
return count($this->messages); | |
} | |
public function at(int $idx) : array | |
{ | |
if (!isset($this->messages[$idx])) { | |
throw new OutOfBoundsException(); | |
} | |
return $this->messages[$idx]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment