feat(logger): setup logger with discord and db logging

This commit is contained in:
2025-08-31 21:35:50 -05:00
parent fc3cfe999a
commit 2e51474a5e
13 changed files with 1577 additions and 20 deletions

View File

@@ -0,0 +1,34 @@
import build from "pino-abstract-transport";
import { db } from "../db/db.js";
import { logs, type Log } from "../db/schema/logs.js";
const pinoLogLevels: any = {
10: "trace",
20: "debug",
30: "info",
40: "warn",
50: "error",
60: "fatal",
};
// Create a custom transport function
export default async function (log: Log) {
//const {username, service, level, msg, ...extra} = log;
try {
return build(async function (source) {
for await (let obj of source) {
// convert to the name to make it more easy to find later :P
const levelName = pinoLogLevels[obj.level] || "unknown";
await db.insert(logs).values({
level: levelName,
module: obj?.module.toLowerCase(),
subModule: obj?.subModule.toLowerCase(),
hostname: obj?.hostname.toLowerCase(),
message: obj.msg,
stack: obj?.stack,
});
}
});
} catch (err) {
console.error("Error inserting log into database:", err);
}
}