feat(logger): added transport to post to db instead of file

This commit is contained in:
2025-03-07 13:39:53 -06:00
parent ef26b6aa79
commit ce11b1f57e
2 changed files with 47 additions and 20 deletions

View File

@@ -0,0 +1,29 @@
import {db} from "../../../database/dbclient.js";
import {logs} from "../../../database/schema/logs.js";
import build from "pino-abstract-transport";
type Log = {
username: string | null;
service: string;
level: string;
msg: string;
};
// 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) {
// Insert log entry into the PostgreSQL database using Drizzle ORM
await db.insert(logs).values({
level: obj.level,
username: obj?.username,
service: obj?.service,
message: obj.msg,
});
}
});
} catch (err) {
console.error("Error inserting log into database:", err);
}
}