spatie/laravel-health
Monitor your Laravel app’s health by registering configurable checks (disk space, queues, cache, etc.). Get warnings or failures and receive notifications via mail or Slack, with an easy API for adding custom checks and reporting status.
Installation:
composer require spatie/laravel-health
Publish the config file:
php artisan vendor:publish --tag="health-config"
First Check:
Register a basic check in a service provider (e.g., AppServiceProvider):
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DatabaseConnectionCheck;
Health::checks([
DatabaseConnectionCheck::new(),
]);
View Results:
Add the health route to your routes/web.php:
use Spatie\Health\Facades\Health;
Health::routes();
Visit /health to see the dashboard.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
Health::checks([
UsedDiskSpaceCheck::new()
->warnWhenUsedSpaceIsAbovePercentage(70)
->failWhenUsedSpaceIsAbovePercentage(90),
]);
/health.config/health.php (e.g., Slack, email).DatabaseConnectionCheck, QueueCheck).
Health::checks([
DatabaseConnectionCheck::new(),
QueueCheck::new()->failWhenJobsAreBehindBy(100),
]);
Spatie\Health\Checks\Check:
use Spatie\Health\Checks\Check;
use Spatie\Health\Result;
class CustomCheck extends Check
{
public function perform(): Result
{
if ($this->someCondition()) {
return Result::fail('Custom failure message');
}
return Result::ok();
}
}
Register it:
Health::checks([new CustomCheck()]);
config/health.php:
'notifications' => [
Spatie\Health\Notifications\CheckFailedNotification::class => ['mail', 'slack'],
],
throttle_notifications_for_minutes (default: 60).only_on_failure to ignore warnings:
'notifications' => [
'enabled' => true,
'only_on_failure' => true,
],
// config/health.php
'oh_dear_endpoint' => [
'enabled' => true,
'secret' => env('OH_DEAR_HEALTH_CHECK_SECRET'),
'url' => '/oh-dear-health-check-results',
],
'notifications' => ['enabled' => false],
'result_stores' => [
Spatie\Health\ResultStores\InMemoryHealthResultStore::class,
],
Add to your cron schedule (e.g., app/Console/Kernel.php):
protected function schedule(Schedule $schedule)
{
$schedule->command('health:check')->hourly();
}
Run checks manually:
php artisan health:check
Duplicate Check Names:
DatabaseConnectionCheck). Override with:
DatabaseConnectionCheck::new()->name('Production DB Check')
PingCheck for multiple URLs).Secret Token Misconfiguration:
/health publicly, ensure the secret_token is set in config/health.php:
'secret_token' => env('HEALTH_SECRET_TOKEN'),
Notification Delays:
throttle_notifications_for_minutes or disable for critical checks:
'notifications' => ['throttle_notifications_for_minutes' => 5],
Oh Dear Sync Issues:
always_send_fresh_results is false, Oh Dear may show stale data. Set to true for real-time updates:
'oh_dear_endpoint' => ['always_send_fresh_results' => true],
Check Results Manually:
php artisan health:check --verbose
Inspect Config:
php artisan config:dump Spatie\Health\HealthServiceProvider
Test Notifications:
php artisan notify:send with a test notification class to validate channels.Log Health Checks:
class LogCheck extends Check
{
public function perform(): Result
{
\Log::info('Health check performed', ['result' => $this->result()]);
return $this->result();
}
}
Custom Result Stores:
Spatie\Health\ResultStores\HealthResultStore to persist results (e.g., database):
class DatabaseHealthResultStore implements HealthResultStore
{
public function store(HealthResult $result): void
{
// Save to DB
}
}
config/health.php:
'result_stores' => [App\ResultStores\DatabaseHealthResultStore::class],
Dynamic Checks:
$checks = config('health.checks', []);
Health::checks(array_map(fn ($check) => $check::new(), $checks));
Webhook Triggers:
Route::post('/trigger-health-check', function () {
$result = Health::checkNow();
return response()->json($result);
});
Slack/Discord Rich Messages:
CheckFailedNotification:
use Spatie\Health\Notifications\CheckFailedNotification;
class CustomCheckFailedNotification extends CheckFailedNotification
{
public function toSlack($notifiable)
{
return 'Custom Slack message: ' . $this->message;
}
}
config/health.php:
'notifications' => [
App\Notifications\CustomCheckFailedNotification::class => ['slack'],
],
How can I help you explore Laravel packages today?