Laravel Log Fake 2.0
Published on by Paul Redmond
Laravel Log Fake released v2.0 with support for Laravel 9 and a completely closure-based assertion API:
Finally tagged a major version (v2) of my LogFake package
— Tim MacDonald (@timacdonald87) May 8, 2022
- Laravel 9 Support (🙈)
- Re-write of the log assertion API
- Context support + assertions against a channels context
- Inspection capabilities (dump, dd)
- Support for custom messages
Enjoy 🍻https://t.co/Lbi56PTGyX
The Log fake package is an excellent way to ensure critical logging happens through testing assertions. As a basic example from the readme, you first bind the LogFake
instance, and then you can assert logging:
LogFake::bind(); // Run test code // Logging assertionsLog::assertLogged(fn (LogEntry $log) => $log->level === 'info' && $log->message === 'User logged in.' && $log->context === ['user_id' => 5]);
Also new in v2.0 are the dd()
and dump()
helpers to debug log messages during a test:
Log::dump();// array:1 [// 0 => array:4 [// "level" => "info"// "message" => "User logged in."// "context" => []// "channel" => "stack"// ]// ] Log::channel('slack')->dump(); Log::dumpAll(); Log::dd();Log::ddAll();
You can also inspect logging and write assertions around them using various logs()
helpers:
$logs = Log::allLogs(); assert($logs->count() === 2); $logs = Log::allLogs();assert($logs->count() === 2);
Lastly, along with Laravel 9.12's per-channel logging context API, the Log Fake package can also assert context:
Log::assertCurrentContext( fn (array $context) => $context['app'] === 'Acme CRM'));
Be sure the check out the available assertions to see the complete log assertion API available. You can learn more about this package, get full installation instructions, and view the source code on GitHub.