spatie/laravel-health
Monitor your Laravel app’s health by registering checks (disk space, etc.) with warning/fail thresholds. Get notified via mail or Slack when checks degrade, and extend with custom checks for proactive alerting.
This package provides methods to run certain checks only when specified conditions are met.
If you would like to conditionally run a check, you can use the if and unless methods.
For more control, you can also use callables. They are evaluated every time a health check is run.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;
Health::checks([
DebugModeCheck::new()->unless(app()->environment('local')),
RedisCheck::new()->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()),
]);
Important!
Be cautious when conditionally running or modifying checks. By default, skipped checks are considered failures, which may impact the overall health status. You can adjust this behavior by setting the configuration optiontreat_skipped_as_failuretofalsein the health config file.
You may find yourself repeating conditions for multiple checks. To avoid that, you can register a Laravel macro on a check with a custom condition method.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;
Check::macro('ifEnvironment', fn (string|array $envs) => $this->if(fn () => app()->environment($envs)));
Health::checks([
DebugModeCheck::new()->ifEnvironment('production')
]);
Sometimes you need more than one condition on a check, so you may chain two or more of them
simply by calling if or unless multiple times. They are evaluated in the order that
you define them.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DebugModeCheck;
use Spatie\Health\Checks\Checks\RedisCheck;
Health::checks([
DebugModeCheck::new()
->unless(app()->environment('local'))
->if(fn () => app(SomeHeavyService::class)->shouldCheckHealth()),
]);
You may want to slightly change check's configuration under a specific condition. You can do
so using when and doUnless methods. In this example, a smaller memory limit is enforced
on a local environment.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck;
Health::checks([
RedisMemoryUsageCheck::new()
->failWhenAboveMb(1000)
->when(
app()->environment('local'),
fn (RedisMemoryUsageCheck $check) => $check->failWhenAboveMb(200)
),
]);
How can I help you explore Laravel packages today?