refactor(logging): when notify is true send the error to systemAdmins

This commit is contained in:
2026-04-10 10:32:20 -05:00
parent d05a0ce930
commit 79e653efa3
3 changed files with 53 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import { db } from "../db/db.controller.js";
import { logs } from "../db/schema/logs.schema.js";
import { emitToRoom } from "../socket.io/roomEmitter.socket.js";
import { tryCatch } from "../utils/trycatch.utils.js";
import { notifySystemIssue } from "./logger.notify.js";
//import build from "pino-abstract-transport";
export const logLevel = process.env.LOG_LEVEL || "info";
@@ -45,6 +46,10 @@ const dbStream = new Writable({
console.error(res.error);
}
if (obj.notify) {
notifySystemIssue(obj);
}
if (obj.room) {
emitToRoom(obj.room, res.data ? res.data[0] : obj);
}

View File

@@ -0,0 +1,44 @@
/**
* For all logging that has notify set to true well send an email to the system admins, if we have a discord webhook set well send it there as well
*/
import { eq } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import { user } from "../db/schema/auth.schema.js";
import { sendEmail } from "../utils/sendEmail.utils.js";
type NotifyData = {
module: string;
submodule: string;
hostname: string;
msg: string;
stack: unknown[];
};
export const notifySystemIssue = async (data: NotifyData) => {
// build the email out
const formattedError = Array.isArray(data.stack)
? data.stack.map((e: any) => e.error || e)
: data.stack;
const sysAdmin = await db
.select()
.from(user)
.where(eq(user.role, "systemAdmin"));
await sendEmail({
email: sysAdmin.map((r) => r.email).join("; ") ?? "cowchmonkey@gmail.com", // change to pull in system admin emails
subject: `${data.hostname} has encountered a critical issue.`,
template: "serverCritialIssue",
context: {
plant: data.hostname,
module: data.module,
subModule: data.submodule,
message: data.msg,
error: JSON.stringify(formattedError, null, 2),
},
});
// TODO: add discord
};

View File

@@ -23,7 +23,7 @@ const reprint = async (data: any, emails: string) => {
module: "notification",
subModule: "query",
message: `${data.name} encountered an error while trying to get initial info`,
data: [le],
data: le as any,
notify: true,
});
}
@@ -52,7 +52,7 @@ const reprint = async (data: any, emails: string) => {
module: "notification",
subModule: "query",
message: `Data for: ${l[0].name} encountered an error while trying to get it`,
data: [error],
data: error as any,
notify: true,
});
}
@@ -73,7 +73,7 @@ const reprint = async (data: any, emails: string) => {
module: "notification",
subModule: "query",
message: `Data for: ${l[0].name} encountered an error while trying to get it`,
data: [dbe],
data: dbe as any,
notify: true,
});
}
@@ -90,22 +90,13 @@ const reprint = async (data: any, emails: string) => {
});
if (!sentEmail?.success) {
// sendEmail({
// email: "Blake.matths@alpla.com",
// subject: `${os.hostname()} failed to run ${data[0]?.name}.`,
// template: "serverCrash",
// context: {
// error: sentEmail?.data,
// plant: `${os.hostname()}`,
// },
// });
return returnFunc({
success: false,
level: "error",
module: "notification",
subModule: "email",
message: `${l[0].name} failed to send the email`,
data: [sentEmail?.data],
data: sentEmail?.data as any,
notify: true,
});
}