Package Data | |
---|---|
Maintainer Username: | timacdonald |
Maintainer Contact: | hello@timacdonald.me (Tim MacDonald) |
Package Create Date: | 2018-10-06 |
Package Last Update: | 2024-08-05 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:01:19 |
Package Statistics | |
---|---|
Total Downloads: | 3,325,058 |
Monthly Downloads: | 110,586 |
Daily Downloads: | 4,625 |
Total Stars: | 404 |
Total Watchers: | 7 |
Total Forks: | 31 |
Total Open Issues: | 1 |
A bunch of Laravel facades / services are able to be faked, such as the Dispatcher with Bus::fake()
, to help with testing and assertions. This package gives you the ability to fake the logger in your app, and includes the ability to make assertions against channels and stacks introduced in logging overhaul in Laravel 5.6
.
You can install using composer from Packagist
$ composer require timacdonald/log-fake --dev
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');
});
If you are logging to a specific channel in your app, such as Slack with Log::channel('slack')->critical('It is 5pm, go home')
, you need to also prefix your assertions in the same manner.
use TiMacDonald\Log\LogFake;
use Illuminate\Support\Facades\Log;
//...
Log::swap(new LogFake);
Log::channel('slack')->alert('It is 5pm, go home');
Log::channel('slack')->assertLogged('alert'); // ✅ passes
// without the channel prefix...
Log::assertLogged('alert'); // ❌ fails
If you are logging to a stack in your app, like with channels, you will need to prefix your assertions. Note that the order of the stack does not matter.
use TiMacDonald\Log\LogFake;
use Illuminate\Support\Facades\Log;
//...
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
All assertions are relative to the channel or stack as shown in the previous examples.
Log::assertLogged('info');
Log::channel('slack')->assertLogged('alert');
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical');
// with a callback
Log::assertLogged('info', function ($message, $context) {
return str_contains($message, 'Donuts');
});
Log::channel('slack')->assertLogged('alert', function ($message, $context) {
return str_contains($message, '5pm');
});
Log::stack(['bugsnag', 'sentry'])->assertLogged('critical', function ($message, $context) {
return str_contains($message, 'evasive maneuvers');
});
Log::assertLoggedTimes('info', 5);
Log::channel('slack')->assertLoggedTimes('alert', 5);
Log::stack(['bugsnag', 'sentry'])->assertLoggedTimes('critical', 5);
Log::assertNotLogged('info');
Log::channel('slack')->assertNotLogged('alert');
Log::stack(['bugsnag', 'sentry'])->assertNotLogged('critical');
// with a callback
Log::assertNotLogged('info', function ($message, $context) {
return str_contains($message, 'Donuts');
});
Log::channel('slack')->assertNotLogged('alert' , function ($message, $context) {
return str_contains($message, '5pm');
});
Log::stack(['bugsnag', 'sentry'])->assertNotLogged('critical', function ($message, $context) {
return str_contains($message, 'evasive maneuvers');
});
Log::assertNothingLogged();
Log::channel('slack')->assertNothingLogged();
Log::stack(['bugsnag', 'sentry'])->assertNothingLogged();
You are free to use this package, but I ask that you reach out to someone (not me) who has previously, or is currently, maintaining or contributing to an open source library you are using in your project and thank them for their work. Consider your entire tech stack: packages, frameworks, languages, databases, operating systems, frontend, backend, etc.