Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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