From c28ec4921e58cd9a8dbf37c8a5a326e081b77e91 Mon Sep 17 00:00:00 2001 From: Hosteroid Date: Fri, 17 Oct 2025 11:30:59 +0300 Subject: [PATCH] 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. --- app/Services/Channels/DiscordChannel.php | 19 +++++++++++++++++-- app/Services/Channels/SlackChannel.php | 19 +++++++++++++++++-- app/Services/Channels/TelegramChannel.php | 22 ++++++++++++++++++++-- app/Services/Channels/WebhookChannel.php | 22 ++++++++++++++++++++-- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/app/Services/Channels/DiscordChannel.php b/app/Services/Channels/DiscordChannel.php index 8b826ea..ce0bf74 100644 --- a/app/Services/Channels/DiscordChannel.php +++ b/app/Services/Channels/DiscordChannel.php @@ -3,14 +3,17 @@ namespace App\Services\Channels; use GuzzleHttp\Client; +use App\Services\Logger; class DiscordChannel implements NotificationChannelInterface { private Client $client; + private Logger $logger; public function __construct() { $this->client = new Client(['timeout' => 10]); + $this->logger = new Logger('discord_channel'); } public function send(array $config, string $message, array $data = []): bool @@ -28,9 +31,21 @@ class DiscordChannel implements NotificationChannelInterface ] ]); - return $response->getStatusCode() === 204; + $ok = $response->getStatusCode() === 204; + if ($ok) { + $this->logger->info('Discord message sent', [ + 'status' => $response->getStatusCode() + ]); + } else { + $this->logger->error('Discord non-204 status', [ + 'status' => $response->getStatusCode() + ]); + } + return $ok; } catch (\Exception $e) { - error_log("Discord send failed: " . $e->getMessage()); + $this->logger->error('Discord send failed', [ + 'exception' => $e->getMessage() + ]); return false; } } diff --git a/app/Services/Channels/SlackChannel.php b/app/Services/Channels/SlackChannel.php index 6586773..176b106 100644 --- a/app/Services/Channels/SlackChannel.php +++ b/app/Services/Channels/SlackChannel.php @@ -3,14 +3,17 @@ namespace App\Services\Channels; use GuzzleHttp\Client; +use App\Services\Logger; class SlackChannel implements NotificationChannelInterface { private Client $client; + private Logger $logger; public function __construct() { $this->client = new Client(['timeout' => 10]); + $this->logger = new Logger('slack_channel'); } public function send(array $config, string $message, array $data = []): bool @@ -29,9 +32,21 @@ class SlackChannel implements NotificationChannelInterface 'json' => $payload ]); - return $response->getStatusCode() === 200; + $ok = $response->getStatusCode() === 200; + if ($ok) { + $this->logger->info('Slack message sent', [ + 'status' => $response->getStatusCode() + ]); + } else { + $this->logger->error('Slack non-200 status', [ + 'status' => $response->getStatusCode() + ]); + } + return $ok; } catch (\Exception $e) { - error_log("Slack send failed: " . $e->getMessage()); + $this->logger->error('Slack send failed', [ + 'exception' => $e->getMessage() + ]); return false; } } diff --git a/app/Services/Channels/TelegramChannel.php b/app/Services/Channels/TelegramChannel.php index 27f8239..1084e00 100644 --- a/app/Services/Channels/TelegramChannel.php +++ b/app/Services/Channels/TelegramChannel.php @@ -3,10 +3,12 @@ namespace App\Services\Channels; use GuzzleHttp\Client; +use App\Services\Logger; class TelegramChannel implements NotificationChannelInterface { private Client $client; + private Logger $logger; public function __construct() { @@ -14,6 +16,7 @@ class TelegramChannel implements NotificationChannelInterface 'base_uri' => 'https://api.telegram.org', 'timeout' => 10, ]); + $this->logger = new Logger('telegram_channel'); } public function send(array $config, string $message, array $data = []): bool @@ -32,9 +35,24 @@ class TelegramChannel implements NotificationChannelInterface ] ]); - return $response->getStatusCode() === 200; + $ok = $response->getStatusCode() === 200; + if ($ok) { + $this->logger->info('Telegram message sent', [ + 'chat_id' => $config['chat_id'], + 'status' => $response->getStatusCode() + ]); + } else { + $this->logger->error('Telegram non-200 status', [ + 'chat_id' => $config['chat_id'], + 'status' => $response->getStatusCode() + ]); + } + return $ok; } catch (\Exception $e) { - error_log("Telegram send failed: " . $e->getMessage()); + $this->logger->error('Telegram send failed', [ + 'chat_id' => $config['chat_id'] ?? null, + 'exception' => $e->getMessage() + ]); return false; } } diff --git a/app/Services/Channels/WebhookChannel.php b/app/Services/Channels/WebhookChannel.php index 7900f7a..fdeaa60 100644 --- a/app/Services/Channels/WebhookChannel.php +++ b/app/Services/Channels/WebhookChannel.php @@ -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; } }