Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Psr Testlogger Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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']);

Implementation Patterns

  • Dependency Injection: Pass 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.
  • Per-Test Isolation: Create a fresh TestLogger instance in each test (setUp() or per-test) to avoid cross-test contamination.
  • Fluent Assertions: Use built-in helpers like assertRecordExists(), assertRecordCount(), assertLogCountByLevel(), and assertLogHasMessage() for expressive, readable test assertions.
  • Context Verification: Validate complex context structures by accessing getRecords() and inspecting the context array—ideal for testing structured logging (e.g., user ID, request ID).
  • Mock Replacement: Avoid heavy monolog setup; swap TestLogger for production loggers in tests to ensure zero I/O and deterministic results.

Gotchas and Tips

  • Thread Safety: 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).
  • Record Retention: Records persist in memory for the lifetime of the logger instance—always instantiate a new logger per test to avoid false positives.
  • Strict Assertion Matching: assertRecordExists() uses strpos() by default for message matching (not ==). Use the optional $strict parameter to force exact matches.
  • Extensibility: Extend TestLogger to add project-specific helpers (e.g., assertUserActivityLog()) without duplicating logic across tests.
  • PHPUnit Integration Tip: In tearDown(), call $logger->clear() or rely on re-instantiation—whichever keeps tests cleaner. Avoid global static instances.
  • Context Arrays: Ensure all context keys match expected types (e.g., string, int) since TestLogger stores raw arrays—no normalization occurs.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport