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:
@@ -3,14 +3,17 @@
|
|||||||
namespace App\Services\Channels;
|
namespace App\Services\Channels;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use App\Services\Logger;
|
||||||
|
|
||||||
class DiscordChannel implements NotificationChannelInterface
|
class DiscordChannel implements NotificationChannelInterface
|
||||||
{
|
{
|
||||||
private Client $client;
|
private Client $client;
|
||||||
|
private Logger $logger;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->client = new Client(['timeout' => 10]);
|
$this->client = new Client(['timeout' => 10]);
|
||||||
|
$this->logger = new Logger('discord_channel');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(array $config, string $message, array $data = []): bool
|
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) {
|
} catch (\Exception $e) {
|
||||||
error_log("Discord send failed: " . $e->getMessage());
|
$this->logger->error('Discord send failed', [
|
||||||
|
'exception' => $e->getMessage()
|
||||||
|
]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,17 @@
|
|||||||
namespace App\Services\Channels;
|
namespace App\Services\Channels;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use App\Services\Logger;
|
||||||
|
|
||||||
class SlackChannel implements NotificationChannelInterface
|
class SlackChannel implements NotificationChannelInterface
|
||||||
{
|
{
|
||||||
private Client $client;
|
private Client $client;
|
||||||
|
private Logger $logger;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->client = new Client(['timeout' => 10]);
|
$this->client = new Client(['timeout' => 10]);
|
||||||
|
$this->logger = new Logger('slack_channel');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(array $config, string $message, array $data = []): bool
|
public function send(array $config, string $message, array $data = []): bool
|
||||||
@@ -29,9 +32,21 @@ class SlackChannel implements NotificationChannelInterface
|
|||||||
'json' => $payload
|
'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) {
|
} catch (\Exception $e) {
|
||||||
error_log("Slack send failed: " . $e->getMessage());
|
$this->logger->error('Slack send failed', [
|
||||||
|
'exception' => $e->getMessage()
|
||||||
|
]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,12 @@
|
|||||||
namespace App\Services\Channels;
|
namespace App\Services\Channels;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use App\Services\Logger;
|
||||||
|
|
||||||
class TelegramChannel implements NotificationChannelInterface
|
class TelegramChannel implements NotificationChannelInterface
|
||||||
{
|
{
|
||||||
private Client $client;
|
private Client $client;
|
||||||
|
private Logger $logger;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@@ -14,6 +16,7 @@ class TelegramChannel implements NotificationChannelInterface
|
|||||||
'base_uri' => 'https://api.telegram.org',
|
'base_uri' => 'https://api.telegram.org',
|
||||||
'timeout' => 10,
|
'timeout' => 10,
|
||||||
]);
|
]);
|
||||||
|
$this->logger = new Logger('telegram_channel');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(array $config, string $message, array $data = []): bool
|
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) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,17 @@
|
|||||||
namespace App\Services\Channels;
|
namespace App\Services\Channels;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use App\Services\Logger;
|
||||||
|
|
||||||
class WebhookChannel implements NotificationChannelInterface
|
class WebhookChannel implements NotificationChannelInterface
|
||||||
{
|
{
|
||||||
private Client $httpClient;
|
private Client $httpClient;
|
||||||
|
private Logger $logger;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->httpClient = new Client(['timeout' => 10]);
|
$this->httpClient = new Client(['timeout' => 10]);
|
||||||
|
$this->logger = new Logger('webhook_channel');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(array $config, string $message, array $data = []): bool
|
public function send(array $config, string $message, array $data = []): bool
|
||||||
@@ -37,9 +40,24 @@ class WebhookChannel implements NotificationChannelInterface
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$status = $response->getStatusCode();
|
$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) {
|
} catch (\Exception $e) {
|
||||||
error_log('Webhook send failed: ' . $e->getMessage());
|
$this->logger->error('Webhook send failed', [
|
||||||
|
'url' => $url,
|
||||||
|
'exception' => $e->getMessage()
|
||||||
|
]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user