Skip to content

Instantly share code, notes, and snippets.

Last active October 20, 2021 14:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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