Laravel Packages

Laravel Log Fake 2.0

Published
Laravel Log Fake 2.0 image

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:

LogFake::bind();
 
// Run test code
 
// Logging assertions
Log::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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article