refactor(logger): added in the db posting

This commit is contained in:
2025-12-29 06:26:40 -06:00
parent ea72fd10cd
commit 9efd6419b6
9 changed files with 1475 additions and 18 deletions

View File

@@ -0,0 +1,28 @@
import {
boolean,
jsonb,
pgTable,
text,
timestamp,
uuid,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type { z } from "zod";
export const logs = pgTable("logs", {
id: uuid("id").defaultRandom().primaryKey(),
level: text("level"),
module: text("module").notNull(),
subModule: text("subModule"),
message: text("message").notNull(),
stack: jsonb("stack").default([]),
checked: boolean("checked").default(false),
hostname: text("hostname"),
createdAt: timestamp("createdAt").defaultNow(),
});
export const logSchema = createSelectSchema(logs);
export const newLogSchema = createInsertSchema(logs);
export type Log = z.infer<typeof logSchema>;
export type NewLog = z.infer<typeof newLogSchema>;

View File

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

View File

@@ -12,6 +12,9 @@ const transport = pino.transport({
destination: process.stdout.fd,
},
},
{
target: "./db.transport.ts",
},
],
});

10
drizzle.config.ts Normal file
View File

@@ -0,0 +1,10 @@
import { defineConfig } from "drizzle-kit";
const dbURL = `postgres://${process.env.DATABASE_USER}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}:${process.env.DATABASE_PORT}/${process.env.DATABASE_DB}`;
export default defineConfig({
dialect: "postgresql",
schema: "./dist/backend/src/db/schema",
out: "./migrations",
dbCredentials: { url: dbURL },
});

View File

@@ -0,0 +1,11 @@
CREATE TABLE "logs" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"level" text,
"module" text NOT NULL,
"subModule" text,
"message" text NOT NULL,
"stack" jsonb DEFAULT '[]'::jsonb,
"checked" boolean DEFAULT false,
"hostname" text,
"createdAt" timestamp DEFAULT now()
);

View File

@@ -0,0 +1,90 @@
{
"id": "a42ab1d2-29ee-43ca-bcad-7b14c545172e",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.logs": {
"name": "logs",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"level": {
"name": "level",
"type": "text",
"primaryKey": false,
"notNull": false
},
"module": {
"name": "module",
"type": "text",
"primaryKey": false,
"notNull": true
},
"subModule": {
"name": "subModule",
"type": "text",
"primaryKey": false,
"notNull": false
},
"message": {
"name": "message",
"type": "text",
"primaryKey": false,
"notNull": true
},
"stack": {
"name": "stack",
"type": "jsonb",
"primaryKey": false,
"notNull": false,
"default": "'[]'::jsonb"
},
"checked": {
"name": "checked",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
},
"hostname": {
"name": "hostname",
"type": "text",
"primaryKey": false,
"notNull": false
},
"createdAt": {
"name": "createdAt",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1766759965948,
"tag": "0000_ordinary_justice",
"breakpoints": true
}
]
}

1282
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,9 @@
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "dotenvx run -f .env -- npm run dev:app",
"dev:app": "cd backend && tsx watch app.ts",
"build": "npm run specCheck && npm run lint && npm run build:app",
"dev:db:migrate": "npx drizzle-kit push",
"dev:db:generate": "tsc && npx drizzle-kit generate --config=drizzle.config.ts",
"build": "npm run specCheck && npm run lint && npm run dev:db:generate && npm run dev:db:migrate && npm run build:app && rimraf dist/backend",
"build:app": "ncc build backend/app.ts -o dist -m -s",
"lint": "tsc && biome lint",
"start": "dotenvx run -f .env -- node dist/index.js",
@@ -16,7 +18,6 @@
"version": "changeset version",
"release": "dotenvx run -f .env -- npm run version && git push --follow-tags && node scripts/create-release.js",
"specCheck": "node scripts/check-route-specs.mjs"
},
"repository": {
"type": "git",
@@ -36,6 +37,7 @@
"@types/morgan": "^1.9.10",
"@types/mssql": "^9.1.8",
"@types/node": "^24.10.1",
"@types/pg": "^8.16.0",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.8",
"@vercel/ncc": "^0.38.4",
@@ -43,12 +45,16 @@
"better-auth": "^1.4.6",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"drizzle-kit": "^0.31.8",
"drizzle-orm": "^0.45.1",
"drizzle-zod": "^0.8.3",
"express": "^5.2.1",
"husky": "^8.0.3",
"morgan": "^1.10.1",
"mssql": "^12.2.0",
"npm-check-updates": "^19.1.2",
"openapi-types": "^12.1.3",
"pg": "^8.16.3",
"pino": "^10.1.0",
"pino-pretty": "^13.1.3",
"ts-node-dev": "^2.0.0",
@@ -56,7 +62,8 @@
"typescript": "^5.9.3"
},
"dependencies": {
"@dotenvx/dotenvx": "^1.51.2"
"@dotenvx/dotenvx": "^1.51.2",
"postgres": "^3.4.7"
},
"config": {
"commitizen": {