Skip to content

Commit 525f28d

Browse files
committed
Add logging of healthcheck status
1 parent 73e4e61 commit 525f28d

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

config/healthcheck.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
'scheduler' => false,
1414

15+
'logging' => true,
16+
1517
];

src/HealthCheckController.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ public function __invoke(): JsonResponse
2020

2121
$response = [];
2222

23+
$logContext = [];
24+
2325
// Check database connection
2426
if (config('healthcheck.database', true) === true) {
2527
try {
2628
DB::connection()->getPdo();
2729
$dbStatus = 'OK';
30+
$logContext['is_database_connected'] = true;
2831
} catch (Exception $e) {
2932
$status = 500;
30-
// Add logs for cloudwatch to monitor
31-
Log::error('Unable to connect to Database', [
32-
'is_database_connected' => false,
33-
'include_in_metrics' => false,
34-
]);
33+
$logContext['is_database_connected'] = false;
3534
$dbStatus = 'Unable to connect to the database: ' . $e->getMessage();
3635
}
3736
$response['db_status'] = $dbStatus;
@@ -42,15 +41,11 @@ public function __invoke(): JsonResponse
4241
try {
4342
Redis::ping();
4443
$redisStatus = 'OK';
44+
$logContext['is_redis_connected'] = true;
4545
} catch (Exception $e) {
4646
$status = 500;
4747
$redisStatus = 'Unable to connect to Redis: ' . $e->getMessage();
48-
49-
// Add logs for cloudwatch to monitor
50-
Log::error('Unable to connect to Redis', [
51-
'is_redis_connected' => false,
52-
'include_in_metrics' => false,
53-
]);
48+
$logContext['is_redis_connected'] = false;
5449
}
5550
$response['redis_status'] = $redisStatus;
5651
}
@@ -63,33 +58,33 @@ public function __invoke(): JsonResponse
6358
$horizonStatus = 'Horizon is currently ' . $horizonCurrentStatus;
6459
if ($horizonCurrentStatus === 'running') {
6560
$horizonStatus = 'OK';
61+
$logContext['is_horizon_running'] = true;
6662
}
6763
}
6864
if ($horizonStatus !== 'OK') {
6965
// Add logs for cloudwatch to monitor
70-
Log::error($horizonStatus, [
71-
'is_horizon_running' => false,
72-
'include_in_metrics' => false,
73-
]);
66+
$logContext['is_horizon_running'] = false;
7467
}
7568
$response['horizon_status'] = $horizonStatus;
7669
}
7770

7871
if (config('healthcheck.scheduler', false)) {
7972
$response['scheduler_status'] = 'OK';
8073
$response['scheduler_last_run'] = $this->getLastRun()?->format('Y-m-d H:i:s');
74+
$logContext['is_scheduler_running'] = true;
75+
$logContext['scheduler_last_run'] = $response['scheduler_last_run'];
8176

8277
// Check if the last run was more than or equal to 2 minutes ago
8378
if (empty($this->getLastRun()) || $this->getLastRun()?->diffInMinutes(Carbon::now()) >= 2) {
8479
$response['scheduler_status'] = 'Scheduler is not running';
85-
Log::error('Scheduler is not running.', [
86-
'is_scheduler_running' => false,
87-
'last_run' => $this->getLastRun()?->format('Y-m-d H:i:s'),
88-
'include_in_metrics' => false,
89-
]);
80+
$logContext['is_scheduler_running'] = false;
9081
}
9182
}
9283

84+
if (config('healthcheck.logging', true)) {
85+
Log::info('Healthcheck status', $logContext);
86+
}
87+
9388
// Assign status to the response but put it in the beginning of the array
9489
$response = array_merge(['status' => $status], $response);
9590

0 commit comments

Comments
 (0)