From 5f17520ba0e7cbda174f8bfa3ed595effec64c24 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 19 Jan 2026 22:16:33 +0000 Subject: [PATCH] logging --- .../notificationProviders/discord.ts | 29 ++++++++++++++----- .../notificationProviders/email.ts | 27 +++++++++++++---- .../notificationProviders/matrix.ts | 27 ++++++++++++----- .../notificationProviders/pagerduty.ts | 26 +++++++++++++---- .../notificationProviders/slack.ts | 29 ++++++++++++++----- .../notificationProviders/webhook.ts | 27 ++++++++++++----- .../infrastructure/notificationsService.ts | 1 - 7 files changed, 124 insertions(+), 42 deletions(-) diff --git a/server/src/service/infrastructure/notificationProviders/discord.ts b/server/src/service/infrastructure/notificationProviders/discord.ts index 83d8e4ec2..ad1f2f28a 100644 --- a/server/src/service/infrastructure/notificationProviders/discord.ts +++ b/server/src/service/infrastructure/notificationProviders/discord.ts @@ -1,10 +1,15 @@ +const SERVICE_NAME = "DiscordProvider"; import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; import { buildHardwareAlerts, buildDiscordBody, getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import got from "got"; export class DiscordProvider implements INotificationProvider { - constructor(private logger: any) {} + private logger: any; + + constructor(logger: any) { + this.logger = logger; + } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); return discordPayload; @@ -23,17 +28,21 @@ export class DiscordProvider implements INotificationProvider { } try { - this.logger?.debug?.("Sending Discord alert", { address: notification.address }); await got.post(notification.address, { json: body, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; - } catch (error: any) { - this.logger?.error?.("Discord alert failed", { error }); + } catch (error) { + const err = error as Error; + this.logger.warn({ + message: "Discord alert failed", + service: SERVICE_NAME, + method: "sendAlert", + stack: err?.stack, + }); return false; } }; @@ -42,17 +51,21 @@ export class DiscordProvider implements INotificationProvider { return false; } try { - this.logger?.debug?.("Sending Discord test alert", { address: notification.address }); await got.post(notification.address, { json: { content: getTestMessage() }, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Discord test alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Discord test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + stack: err?.stack, + }); return false; } }; diff --git a/server/src/service/infrastructure/notificationProviders/email.ts b/server/src/service/infrastructure/notificationProviders/email.ts index 344285e9c..16e48f5c1 100644 --- a/server/src/service/infrastructure/notificationProviders/email.ts +++ b/server/src/service/infrastructure/notificationProviders/email.ts @@ -1,11 +1,15 @@ +const SERVICE_NAME = "EmailProvider"; import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js"; export class EmailProvider implements INotificationProvider { private emailService: any; - constructor(emailService: any, private logger: any) { + private logger: any; + + constructor(emailService: any, logger: any) { this.emailService = emailService; + this.logger = logger; } private buildHardwareEmail = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { @@ -27,18 +31,31 @@ export class EmailProvider implements INotificationProvider { return false; } - this.logger?.debug?.("Sending Email alert", { address: notification.address }); const messageId = await this.emailService.sendEmail(notification.address, subject, html); - if (!messageId) return false; + if (!messageId) { + this.logger.warn({ + message: "Email alert failed", + service: SERVICE_NAME, + method: "sendAlert", + }); + return false; + } return true; } async sendTestAlert(notification: Notification): Promise { const subject = "Test notification"; const html = await buildTestEmail(this.emailService); - this.logger?.debug?.("Sending Email test alert", { address: notification.address }); + const messageId = await this.emailService.sendEmail(notification.address, subject, html); - if (!messageId) return false; + if (!messageId) { + this.logger.warn({ + message: "Email test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + }); + return false; + } return true; } } diff --git a/server/src/service/infrastructure/notificationProviders/matrix.ts b/server/src/service/infrastructure/notificationProviders/matrix.ts index 4592500a5..8914dbf80 100644 --- a/server/src/service/infrastructure/notificationProviders/matrix.ts +++ b/server/src/service/infrastructure/notificationProviders/matrix.ts @@ -1,3 +1,4 @@ +const SERVICE_NAME = "MatrixProvider"; import got from "got"; import type { INotificationProvider } from "@/service/index.js"; import type { Notification, Monitor, MonitorStatusResponse } from "@/types/index.js"; @@ -9,7 +10,11 @@ import { } from "@/service/infrastructure/notificationProviders/utils.js"; export class MatrixProvider implements INotificationProvider { - constructor(private logger: any) {} + private logger: any; + + constructor(logger: any) { + this.logger = logger; + } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); const body = buildHardwareWebhookBody(alertsToSend, monitor); @@ -43,17 +48,21 @@ export class MatrixProvider implements INotificationProvider { formatted_body: formattedMessage, }; try { - this.logger?.debug?.("Sending Matrix alert", { room: roomId }); await got.post(url, { json: body, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Matrix alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Matrix alert failed", + service: SERVICE_NAME, + method: "sendAlert", + stack: err?.stack, + }); return false; } }; @@ -69,17 +78,21 @@ export class MatrixProvider implements INotificationProvider { body: getTestMessage(), }; try { - this.logger?.debug?.("Sending Matrix test alert", { room: roomId }); await got.post(url, { json: body, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Matrix test alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Matrix test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + stack: err?.stack, + }); return false; } }; diff --git a/server/src/service/infrastructure/notificationProviders/pagerduty.ts b/server/src/service/infrastructure/notificationProviders/pagerduty.ts index 2d85cd4d6..f65e50c68 100644 --- a/server/src/service/infrastructure/notificationProviders/pagerduty.ts +++ b/server/src/service/infrastructure/notificationProviders/pagerduty.ts @@ -1,3 +1,4 @@ +const SERVICE_NAME = "PagerDutyProvider"; import got from "got"; import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; @@ -9,7 +10,11 @@ import { } from "@/service/infrastructure/notificationProviders/utils.js"; export class PagerDutyProvider implements INotificationProvider { - constructor(private logger: any) {} + private logger: any; + + constructor(logger: any) { + this.logger = logger; + } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); const body = buildHardwareWebhookBody(alertsToSend, monitor); @@ -30,7 +35,6 @@ export class PagerDutyProvider implements INotificationProvider { } try { - this.logger?.debug?.("Sending PagerDuty alert", { routingKey: notification.address }); await got.post("https://events.pagerduty.com/v2/enqueue", { json: { routing_key: notification.address, @@ -42,19 +46,23 @@ export class PagerDutyProvider implements INotificationProvider { timestamp: new Date().toISOString(), }, }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("PagerDuty alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "PagerDuty alert failed", + service: SERVICE_NAME, + method: "sendAlert", + stack: err?.stack, + }); return false; } } async sendTestAlert(notification: Notification): Promise { try { - this.logger?.debug?.("Sending PagerDuty test alert", { routingKey: notification.address }); await got.post("https://events.pagerduty.com/v2/enqueue", { json: { routing_key: notification.address, @@ -70,7 +78,13 @@ export class PagerDutyProvider implements INotificationProvider { }); return true; } catch (error) { - this.logger?.error?.("PagerDuty test alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "PagerDuty test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + stack: err?.stack, + }); return false; } } diff --git a/server/src/service/infrastructure/notificationProviders/slack.ts b/server/src/service/infrastructure/notificationProviders/slack.ts index 947048e4c..2aeb442eb 100644 --- a/server/src/service/infrastructure/notificationProviders/slack.ts +++ b/server/src/service/infrastructure/notificationProviders/slack.ts @@ -1,3 +1,4 @@ +const SERVICE_NAME = "SlackProvider"; import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; import { @@ -6,10 +7,14 @@ import { buildWebhookBody, getTestMessage, } from "@/service/infrastructure/notificationProviders/utils.js"; -import got from "got"; +import got, { HTTPError } from "got"; export class SlackProvider implements INotificationProvider { - constructor(private logger: any) {} + private logger: any; + + constructor(logger: any) { + this.logger = logger; + } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); const body = buildHardwareWebhookBody(alertsToSend, monitor); @@ -34,17 +39,21 @@ export class SlackProvider implements INotificationProvider { } try { - this.logger?.debug?.("Sending Slack alert", { address: notification.address }); await got.post(notification.address, { json: { text: body }, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Slack alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Slack alert failed", + service: SERVICE_NAME, + method: "sendAlert", + stack: err?.stack, + }); return false; } } @@ -55,17 +64,21 @@ export class SlackProvider implements INotificationProvider { } try { - this.logger?.debug?.("Sending Slack test alert", { address: notification.address }); await got.post(notification.address, { json: { text: getTestMessage() }, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Slack test alert failed", { error }); + const err = error as HTTPError; + this.logger.warn({ + message: "Slack test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + stack: err?.stack, + }); return false; } } diff --git a/server/src/service/infrastructure/notificationProviders/webhook.ts b/server/src/service/infrastructure/notificationProviders/webhook.ts index 3976fb8e5..9e743ae53 100644 --- a/server/src/service/infrastructure/notificationProviders/webhook.ts +++ b/server/src/service/infrastructure/notificationProviders/webhook.ts @@ -1,3 +1,4 @@ +const SERVICE_NAME = "WebhookProvider"; import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; import { @@ -9,7 +10,11 @@ import { import got from "got"; export class WebhookProvider implements INotificationProvider { - constructor(private logger: any) {} + private logger: any; + + constructor(logger: any) { + this.logger = logger; + } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); const body = buildHardwareWebhookBody(alertsToSend, monitor); @@ -34,17 +39,21 @@ export class WebhookProvider implements INotificationProvider { } try { - this.logger?.debug?.("Sending webhook alert", { address: notification.address }); await got.post(notification.address, { json: { text: body }, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Webhook alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Webhook alert failed", + service: SERVICE_NAME, + method: "sendAlert", + stack: err?.stack, + }); return false; } }; @@ -54,17 +63,21 @@ export class WebhookProvider implements INotificationProvider { return false; } try { - this.logger?.debug?.("Sending webhook test alert", { address: notification.address }); await got.post(notification.address, { json: { text: getTestMessage() }, headers: { "Content-Type": "application/json", }, - responseType: "json", }); return true; } catch (error) { - this.logger?.error?.("Webhook test alert failed", { error }); + const err = error as Error; + this.logger.warn({ + message: "Webhook test alert failed", + service: SERVICE_NAME, + method: "sendTestAlert", + stack: err?.stack, + }); return false; } }; diff --git a/server/src/service/infrastructure/notificationsService.ts b/server/src/service/infrastructure/notificationsService.ts index ad27fe99a..7ca52945e 100644 --- a/server/src/service/infrastructure/notificationsService.ts +++ b/server/src/service/infrastructure/notificationsService.ts @@ -140,7 +140,6 @@ export class NotificationsService implements INotificationsService { const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds); const tasks = notifications.map((notification) => this.sendTestNotification(notification)); const outcomes = await Promise.all(tasks); - console.log(outcomes); const succeeded = outcomes.filter(Boolean).length; const failed = outcomes.length - succeeded; if (failed > 0) {