fix: refactoring logs and adding pino logger for production and dev. (#236)

This commit is contained in:
Charles GTE
2026-03-29 11:22:28 +02:00
committed by GitHub
parent 057c192314
commit e1c0fc6917
24 changed files with 354 additions and 120 deletions
+6 -3
View File
@@ -17,12 +17,15 @@ import * as storagePolicy from "@/db/schema/13_storage-policy";
import * as backupStorage from "@/db/schema/14_storage-backup";
import * as healthcheckLog from "@/db/schema/15_healthcheck-log";
const log = logger.child({module: "db"});
import {Pool} from "pg";
// Do not delete
import dotenv from "dotenv";
import {migrate} from "drizzle-orm/node-postgres/migrator";
import {logger} from "@/lib/logger";
dotenv.config({
path: ".env",
@@ -66,12 +69,12 @@ export async function makeMigration() {
const database = drizzle({client: pool});
console.log("Running migrations...");
log.info("Running migrations...");
try {
await migrate(database, {migrationsFolder: "./src/db/migrations"});
console.log("Migrations applied successfully.");
log.info("Migrations applied successfully.");
} catch (error) {
console.error("Error applying migrations:", error);
log.error({error: error}, "Error applying migrations:");
}
}
}
+7 -9
View File
@@ -3,6 +3,9 @@ import * as drizzleDb from "@/db";
import {and, eq, gte, isNotNull, lt} from "drizzle-orm";
import {dispatchNotification} from "@/features/notifications/dispatch";
import {EventPayload} from "@/features/notifications/types";
import {logger} from "@/lib/logger";
const log = logger.child({module: "tasks/healthcheck"});
export async function getHealthLast12hLogs({id}: { id: string }) {
const now = new Date()
@@ -19,7 +22,6 @@ export async function getHealthLast12hLogs({id}: { id: string }) {
)
}
export async function deleteHealthLogsOlderThan12h() {
const now = new Date()
const threshold = new Date(now.getTime() - 12 * 60 * 60 * 1000)
@@ -31,7 +33,7 @@ export async function deleteHealthLogsOlderThan12h() {
lt(drizzleDb.schemas.healthcheckLog.date, threshold)
)
console.log(`Number of logs found to delete: ${logsToDelete.length}`)
log.info({name: "deleteHealthLogsOlderThan12h"},`Number of logs found to delete: ${logsToDelete.length}`)
await db
.delete(drizzleDb.schemas.healthcheckLog)
@@ -56,7 +58,7 @@ export async function checkAgentsHealthError() {
}
if (!settings.defaultNotificationChannelId) {
console.error("No default notification channel id found.");
log.error({name: "checkAgentsHealthError"},`No default notification channel id found.`)
return
}
@@ -89,8 +91,7 @@ export async function checkAgentsHealthError() {
error: "Agent is down",
},
};
console.log("[Agent Healthcheck] :", payload);
log.info({name: "checkAgentsHealthError", payload: payload},`Agent Healthcheck Notification`)
await dispatchNotification(
payload,
@@ -156,7 +157,6 @@ export async function checkDatabasesHealthError() {
continue
}
const promises = policiesToUse.map(alertPolicy => {
const payload: EventPayload = {
@@ -171,15 +171,13 @@ export async function checkDatabasesHealthError() {
},
};
console.log("[Database Healthcheck] :", payload);
log.info({name: "checkDatabasesHealthError", payload: payload},`Database Healthcheck Notification`)
return dispatchNotification(payload, alertPolicy.id == null ? undefined : alertPolicy.id, alertPolicy.id ? undefined : alertPolicy.notificationChannelId, undefined);
});
await Promise.all(promises);
}
}
}
}