spatie/statamic-health
Statamic addon that integrates Spatie Laravel Health to monitor your app with configurable checks (e.g., disk space). View health status in the control panel and get notifications via mail or Slack when checks warn or fail.
Installation:
composer require spatie/statamic-health
Publish the config file (if needed):
php artisan vendor:publish --provider="Spatie\Health\HealthServiceProvider"
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(),
]);
Trigger Health Check: Run the health check via CLI:
php artisan health:check
Or access the web endpoint (if configured):
php artisan health:check --web
View Results: Output will display status (pass/fail/warn) for each check. Example:
✅ Database Connection: OK
⚠️ Disk Space: 85% used (warning threshold: 70%)
Use the package to verify core dependencies like:
DatabaseConnectionCheck)QueueWorkerCheck)UrlCheck)Example:
Health::checks([
DatabaseConnectionCheck::new(),
UrlCheck::new()
->url('https://api.example.com/health')
->failIfStatusCodeIsNot(200),
]);
Registration:
AppServiceProvider@boot() or a dedicated HealthServiceProvider.Configuration:
warnWhenUsedSpaceIsAbovePercentage(70)).config/health.php).Execution:
php artisan health:check in CI/CD pipelines or cron jobs.config/health.php):
'web' => [
'enabled' => true,
'path' => 'health',
],
Access at /health (protected by middleware if needed).Notifications:
Health::checks([
UsedDiskSpaceCheck::new()
->warnWhenUsedSpaceIsAbovePercentage(70)
->failWhenUsedSpaceIsAbovePercentage(90)
->notifyOnFailure(),
]);
Statamic-Specific Checks:
Check class to monitor Statamic components:
use Spatie\Health\Checks\Check;
class StatamicCacheCheck extends Check
{
public function run(): array
{
return [
'status' => Statamic::cache()->has('key') ? 'ok' : 'missing',
];
}
}
Conditional Checks:
if (app()->environment('production')) {
Health::checks([UsedDiskSpaceCheck::new()]);
}
Custom Thresholds:
$threshold = config('health.disk_warn_threshold', 70);
Health::checks([
UsedDiskSpaceCheck::new()->warnWhenUsedSpaceIsAbovePercentage($threshold),
]);
Performance:
parallel helper or queue jobs.Missing Config:
php artisan vendor:publish) may lead to undefined settings.config/health.php.Overlapping Checks:
DatabaseConnectionCheck instances) can cause redundant notifications.AppServiceProvider).Web Endpoint Security:
/health) may expose sensitive data if not protected.Route::middleware(['auth:sanctum'])->get('/health', [HealthCheckController::class, 'index']);
False Positives:
UsedDiskSpaceCheck may trigger warnings during deployments (e.g., temporary high usage).if (!app()->runningInConsole() || !app()->isDownForMaintenance()) {
Health::checks([UsedDiskSpaceCheck::new()]);
}
Check Output:
--verbose flag for detailed logs:
php artisan health:check --verbose
Isolate Issues:
Health::checks([DatabaseConnectionCheck::new()]);
Log Results:
class CustomCheck extends Check
{
protected function run(): array
{
$result = ['status' => 'ok'];
Log::info('Custom check result', $result);
return $result;
}
}
Custom Checks:
Spatie\Health\Checks\Check to create reusable checks:
class RedisHealthCheck extends Check
{
public function run(): array
{
return [
'status' => Redis::ping() ? 'ok' : 'failed',
];
}
}
Environment-Specific Checks:
app()->environment() to enable checks only in specific environments:
if (app()->environment(['staging', 'production'])) {
Health::checks([QueueWorkerCheck::new()]);
}
Notification Channels:
config/health.php:
'notifications' => [
'mail' => true,
'slack' => [
'enabled' => true,
'webhook_url' => env('SLACK_WEBHOOK_URL'),
],
],
Documentation:
README.md in your project to document custom checks and their thresholds for future developers.Testing:
$check = new DatabaseConnectionCheck();
$this->assertEquals('ok', $check->run()['status']);
How can I help you explore Laravel packages today?