News

Log Fake for Laravel

Published
Log Fake for Laravel image

Log Fake is a drop-in fake logger for testing with the Laravel framework by Tim MacDonald (@timacdonald). The Log fake package gives you the ability to fake the logger in your application, including the ability to make assertions against channels and stacks introduced in Laravel 5.6.

Here’s a simple example of using the logger in a test:

<?php
 
use TiMacDonald\Log\LogFake;
use Illuminate\Support\Facades\Log;
 
//...
 
Log::swap(new LogFake);
 
Log::info('Donuts have arrived');
 
Log::assertLogged('info', function ($message, $context) {
return str_contains($message, 'Donuts');
});

As mentioned, this fake supports channels and stacks, so if you are logging to a specific channel in your app, you need to prefix your assertions with a given channel:

<?php
 
Log::channel('slack')->alert('It is 5pm, go home');
 
Log::channel('slack')->assertLogged('alert'); // passes
 
// without the channel prefix...
 
Log::assertLogged('alert'); // fails

Like channels, you will need to do the same thing for stacks:

<?php
 
Log::swap(new LogFake);
 
Log::stack(['bugsnag', 'sentry'])->critical('Perform evasive maneuvers');
 
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical'); // passes
 
// without the stack prefix...
 
Log::assertLogged('critical'); // fails

The GitHub repo has a full list of available assertions and usage instructions, along with the source code.

Nice work Tim!

Paul Redmond photo

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

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

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