feat(logger): added pino in and removed all console logs

This commit is contained in:
2025-12-22 12:46:40 -06:00
parent a8c5aad833
commit 878c3b3638
9 changed files with 520 additions and 28 deletions

View File

@@ -13,5 +13,14 @@
"});"
],
"description": "Insert a returnFunc template"
},
"Create Log factory Template": {
"prefix": "createLog",
"body": [
"createLogger({",
"\tmodule: \"${2:system}\",",
"\tsubModule: \"${2:start up}\",",
"});"
]
}
}

View File

@@ -1,10 +1,12 @@
import express from "express";
import { createLogger } from "./src/logger/logger.controller.js";
import { connectProdSql } from "./src/prodSql/sqlConnection.controller.js";
import { setupRoutes } from "./src/routeHandler.route.js";
const port = Number(process.env.PORT);
export const baseUrl = "";
const startApp = async () => {
const log = createLogger({ module: "system", subModule: "main start" });
const app = express();
// start the connection to the prod sql server
@@ -13,7 +15,7 @@ const startApp = async () => {
setupRoutes(baseUrl, app);
app.listen(port, () => {
console.log(`Listening on port ${port}`);
log.info(`Listening on port ${port}`);
});
};

View File

@@ -0,0 +1,28 @@
import pino, { type Logger } from "pino";
export const logLevel = process.env.LOG_LEVEL || "info";
const transport = pino.transport({
targets: [
{
target: "pino-pretty",
options: {
colorize: true,
singleLine: "true",
destination: process.stdout.fd,
},
},
],
});
const rootLogger: Logger = pino(
{
level: logLevel,
redact: { paths: ["email", "password"], remove: true },
},
transport,
);
export const createLogger = (bindings: Record<string, unknown>): Logger => {
return rootLogger.child(bindings);
};

View File

@@ -1,5 +1,6 @@
import sql from "mssql";
import { prodSqlConfig } from "../configs/prodSql.config.js";
import { createLogger } from "../logger/logger.controller.js";
import { checkHostnamePort } from "../utils/checkHost.utils.js";
import { returnFunc } from "../utils/returnHelper.utils.js";
@@ -9,7 +10,10 @@ export let reconnecting = false;
export const connectProdSql = async () => {
const serverUp = await checkHostnamePort(`${process.env.PROD_SERVER}:1433`);
const log = createLogger({
module: "system",
subModule: "db",
});
if (!serverUp) {
// we will try to reconnect
connected = false;
@@ -37,7 +41,7 @@ export const connectProdSql = async () => {
try {
pool = await sql.connect(prodSqlConfig);
connected = true;
console.log(
log.info(
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
);
} catch (error) {
@@ -54,6 +58,10 @@ export const connectProdSql = async () => {
};
export const closePool = async () => {
const log = createLogger({
module: "system",
subModule: "db",
});
if (!connected) {
return returnFunc({
success: false,
@@ -66,7 +74,7 @@ export const closePool = async () => {
try {
await pool.close();
console.log("Connection pool closed");
log.info("Connection pool closed");
connected = false;
return {
success: true,
@@ -78,6 +86,10 @@ export const closePool = async () => {
}
};
export const reconnectToSql = async () => {
const log = createLogger({
module: "system",
subModule: "db",
});
if (reconnecting) return;
//set reconnecting to true while we try to reconnect
@@ -90,7 +102,7 @@ export const reconnectToSql = async () => {
while (!connected && attempt < maxAttempts) {
attempt++;
console.log(
log.info(
`Reconnect attempt ${attempt}/${maxAttempts} in ${delayStart / 1000}s ...`,
);
@@ -107,7 +119,7 @@ export const reconnectToSql = async () => {
pool = await sql.connect(prodSqlConfig);
reconnecting = false;
connected = true;
console.log(
log.info(
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
);
} catch (error) {
@@ -125,7 +137,8 @@ export const reconnectToSql = async () => {
}
if (!connected) {
console.log(
log.error(
{ notify: true },
"Max reconnect attempts reached on the prodSql server. Stopping retries.",
);

View File

@@ -2,7 +2,7 @@ import type { Express } from "express";
// import the routes and route setups
import { setupApiDocsRoutes } from "./configs/scaler.config.js";
import stats from "./routes/stats.route.js";
import stats from "./system/stats.route.js";
export const setupRoutes = (baseUrl: string, app: Express) => {
//setup all the routes

View File

@@ -1,7 +1,9 @@
import { createLogger } from "../logger/logger.controller.js";
interface Data {
success: boolean;
module: "system" | "ocp";
subModule?: "db" | "labeling" | "printer";
subModule: "db" | "labeling" | "printer";
level: "info" | "error" | "debug" | "fatal";
message: string;
data?: unknown[];
@@ -24,19 +26,20 @@ interface Data {
*/
export const returnFunc = (data: Data) => {
const notify = data.notify ? data.notify : false;
const log = createLogger({ module: data.module, subModule: data.subModule });
// handle the logging part
switch (data.level) {
case "info":
console.log({ notify: notify }, data.message);
log.info({ notify: notify }, data.message);
break;
case "error":
console.log({ notify: notify }, data.message);
log.error({ notify: notify, error: data.data }, data.message);
break;
case "debug":
console.log({ notify: notify }, data.message);
log.debug({ notify: notify }, data.message);
break;
case "fatal":
console.log({ notify: notify }, data.message);
log.fatal({ notify: notify }, data.message);
}
// api section to return

451
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -30,28 +30,30 @@
"@changesets/cli": "^2.27.0",
"@commitlint/cli": "^18.4.0",
"@commitlint/config-conventional": "^18.4.0",
"@scalar/express-api-reference": "^0.8.28",
"@types/express": "^5.0.6",
"@types/mssql": "^9.1.8",
"@types/node": "^24.10.1",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.8",
"@vercel/ncc": "^0.38.4",
"axios": "^1.13.2",
"better-auth": "^1.4.6",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"express": "^5.2.1",
"husky": "^8.0.3",
"mssql": "^12.2.0",
"npm-check-updates": "^19.1.2",
"openapi-types": "^12.1.3",
"pino": "^10.1.0",
"pino-pretty": "^13.1.3",
"ts-node-dev": "^2.0.0",
"tsx": "^4.21.0",
"typescript": "^5.9.3"
},
"dependencies": {
"@dotenvx/dotenvx": "^1.51.2",
"@scalar/express-api-reference": "^0.8.28",
"axios": "^1.13.2",
"better-auth": "^1.4.6",
"express": "^5.2.1",
"mssql": "^12.2.0",
"npm-check-updates": "^19.1.2"
"@dotenvx/dotenvx": "^1.51.2"
},
"config": {
"commitizen": {