colinodell/psr-testlogger
A lightweight PSR-3 TestLogger for unit tests. Capture log records in memory, inspect messages, contexts, and levels, and make assertions without external handlers. Helpful for verifying expected logging behavior in PHPUnit and other test suites.
Install via Composer with composer require --dev colinodell/psr-testlogger. In your test class (e.g., PHPUnit), instantiate ColinODell\PsRTestLogger\TestLogger and inject it wherever a Psr\Log\LoggerInterface is expected. The first use case is asserting that an operation logs a specific message at a given level:
$logger = new TestLogger();
$service = new UserService($logger);
$service->doSomething();
$this->assertEquals('User created', $logger->getRecords()[0]['message']);
$this->assertEquals('info', $logger->getRecords()[0]['level']);
TestLogger wherever LoggerInterface is type-hinted—especially useful in service classes with side-effect methods (e.g., notify(), process()) where logging is part of expected behavior.TestLogger instance in each test (setUp() or per-test) to avoid cross-test contamination.assertRecordExists(), assertRecordCount(), assertLogCountByLevel(), and assertLogHasMessage() for expressive, readable test assertions.getRecords() and inspecting the context array—ideal for testing structured logging (e.g., user ID, request ID).TestLogger for production loggers in tests to ensure zero I/O and deterministic results.TestLogger is not thread-safe—ensure tests run sequentially if using parallel runners (e.g., PHPUnit’s processIsolation is safe; pcov/pthreads may require per-test resets).assertRecordExists() uses strpos() by default for message matching (not ==). Use the optional $strict parameter to force exact matches.TestLogger to add project-specific helpers (e.g., assertUserActivityLog()) without duplicating logic across tests.tearDown(), call $logger->clear() or rely on re-instantiation—whichever keeps tests cleaner. Avoid global static instances.TestLogger stores raw arrays—no normalization occurs.How can I help you explore Laravel packages today?