Laravel Log Fake 2.0

Packages

May 12th, 2022

Laravel Log Fake 2.0

Laravel Log Fake released v2.0 with support for Laravel 9 and a completely closure-based assertion API:

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:

1LogFake::bind();
2 
3// Run test code
4 
5// Logging assertions
6Log::assertLogged(fn (LogEntry $log) =>
7 $log->level === 'info'
8 && $log->message === 'User logged in.'
9 && $log->context === ['user_id' => 5]
10);

Also new in v2.0 are the dd() and dump() helpers to debug log messages during a test:

1Log::dump();
2// array:1 [
3// 0 => array:4 [
4// "level" => "info"
5// "message" => "User logged in."
6// "context" => []
7// "channel" => "stack"
8// ]
9// ]
10 
11Log::channel('slack')->dump();
12 
13Log::dumpAll();
14 
15Log::dd();
16Log::ddAll();

You can also inspect logging and write assertions around them using various logs() helpers:

1$logs = Log::allLogs();
2 
3assert($logs->count() === 2);
4 
5$logs = Log::allLogs();
6assert($logs->count() === 2);

Lastly, along with Laravel 9.12's per-channel logging context API, the Log Fake package can also assert context:

1Log::assertCurrentContext(
2 fn (array $context) => $context['app'] === 'Acme CRM')
3);

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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.