Add structured logging to notification channels

Replaced error_log calls with structured logging using the Logger class in DiscordChannel, SlackChannel, TelegramChannel, and WebhookChannel. Log success and error cases with relevant context for improved observability and debugging.
This commit is contained in:
Hosteroid
2025-10-17 11:30:59 +03:00
parent 2b783b7470
commit c28ec4921e
4 changed files with 74 additions and 8 deletions

View File

@@ -3,14 +3,17 @@
namespace App\Services\Channels;
use GuzzleHttp\Client;
use App\Services\Logger;
class WebhookChannel implements NotificationChannelInterface
{
private Client $httpClient;
private Logger $logger;
public function __construct()
{
$this->httpClient = new Client(['timeout' => 10]);
$this->logger = new Logger('webhook_channel');
}
public function send(array $config, string $message, array $data = []): bool
@@ -37,9 +40,24 @@ class WebhookChannel implements NotificationChannelInterface
]);
$status = $response->getStatusCode();
return $status >= 200 && $status < 300;
$ok = $status >= 200 && $status < 300;
if ($ok) {
$this->logger->info('Webhook sent successfully', [
'url' => $url,
'status' => $status
]);
} else {
$this->logger->error('Webhook responded with non-2xx', [
'url' => $url,
'status' => $status
]);
}
return $ok;
} catch (\Exception $e) {
error_log('Webhook send failed: ' . $e->getMessage());
$this->logger->error('Webhook send failed', [
'url' => $url,
'exception' => $e->getMessage()
]);
return false;
}
}