Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Last active October 20, 2021 14:44
Show Gist options
  • Save chrisguitarguy/04dcfc1622dc8d650a396a79572e293e to your computer and use it in GitHub Desktop.
Save chrisguitarguy/04dcfc1622dc8d650a396a79572e293e to your computer and use it in GitHub Desktop.
<?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