Updated Logging System to Include Application Logs

This commit is contained in:
2026-06-08 12:25:47 -05:00
parent 15e93c1e45
commit 9a9f890494
13 changed files with 323 additions and 12 deletions

View File

@@ -4,6 +4,7 @@
const moment = require('moment');
const crypto = require('crypto');
const { audit } = require('../db');
const { logEvent } = require('../logger');
const { getConfig } = require('./notifications');
const TIMEOUT_MS = 10000;
@@ -74,11 +75,12 @@ async function deliverAlerts(alerts) {
try {
// Note: we build the payload for the alert using the buildPayload function, which creates a stable and flat JSON object with relevant information about the alert, and then we post it to the webhook URL using the postJson function; if the postJson function returns an OK response, we increment the delivered count, otherwise we increment the failed count, allowing us to track the success and failure of each delivery attempt.
const result = await postJson(c.webhookUrl, c.webhookSecret, buildPayload(alert));
if (result.ok) delivered += 1;
else failed += 1;
if (result.ok) { delivered += 1; logEvent('webhook', `Alert delivered: ${alert.title}`, { url: c.webhookUrl, status: result.status, alert_id: alert.id }); }
else { failed += 1; logEvent('webhook', `Alert delivery returned HTTP ${result.status}: ${alert.title}`, { url: c.webhookUrl, status: result.status, alert_id: alert.id }, 'warn'); }
} catch (error) {
console.log(`${moment().format()} ❌ Failed to deliver alert to webhook: ${error.message}`);
failed += 1;
logEvent('webhook', `Alert delivery failed: ${alert.title}${error.message}`, { url: c.webhookUrl, alert_id: alert.id }, 'error');
}
}
console.log(`${moment().format()} 🚚 Webhook delivery complete: ${delivered} delivered, ${failed} failed`);
@@ -108,9 +110,11 @@ async function sendTestWebhook(userId) {
result = await postJson(c.webhookUrl, c.webhookSecret, payload);
} catch (error) {
console.log(`${moment().format()} ❌ Test webhook failed: ${error.message}`);
logEvent('webhook', `Test webhook request failed: ${error.message}`, { url: c.webhookUrl }, 'error');
throw Object.assign(new Error(`Webhook request failed: ${error.message}`), { status: 400 });
}
console.log(`${moment().format()} 📡 Test webhook sent successfully to: ${c.webhookUrl}`) ;
logEvent('webhook', `Test webhook sent (HTTP ${result.status})`, { url: c.webhookUrl, status: result.status }, result.ok ? 'info' : 'warn');
audit(userId, 'send', 'test_webhook', null, null, { url: c.webhookUrl, status: result.status });
if (!result.ok) {
// Note: if the response from the webhook endpoint is not OK (i.e., not in the 200-299 range), we log an error message with the HTTP status code returned by the endpoint and throw an error indicating that the webhook endpoint returned a non-OK status; this allows us to provide clear feedback about why the test webhook failed and helps users understand that their webhook endpoint may be rejecting the request or encountering an error when processing it.