feat(logger): added pino in and removed all console logs
This commit is contained in:
9
.vscode/lst.code-snippets
vendored
9
.vscode/lst.code-snippets
vendored
@@ -13,5 +13,14 @@
|
|||||||
"});"
|
"});"
|
||||||
],
|
],
|
||||||
"description": "Insert a returnFunc template"
|
"description": "Insert a returnFunc template"
|
||||||
|
},
|
||||||
|
"Create Log factory Template": {
|
||||||
|
"prefix": "createLog",
|
||||||
|
"body": [
|
||||||
|
"createLogger({",
|
||||||
|
"\tmodule: \"${2:system}\",",
|
||||||
|
"\tsubModule: \"${2:start up}\",",
|
||||||
|
"});"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
|
import { createLogger } from "./src/logger/logger.controller.js";
|
||||||
import { connectProdSql } from "./src/prodSql/sqlConnection.controller.js";
|
import { connectProdSql } from "./src/prodSql/sqlConnection.controller.js";
|
||||||
import { setupRoutes } from "./src/routeHandler.route.js";
|
import { setupRoutes } from "./src/routeHandler.route.js";
|
||||||
|
|
||||||
const port = Number(process.env.PORT);
|
const port = Number(process.env.PORT);
|
||||||
export const baseUrl = "";
|
export const baseUrl = "";
|
||||||
const startApp = async () => {
|
const startApp = async () => {
|
||||||
|
const log = createLogger({ module: "system", subModule: "main start" });
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// start the connection to the prod sql server
|
// start the connection to the prod sql server
|
||||||
@@ -13,7 +15,7 @@ const startApp = async () => {
|
|||||||
setupRoutes(baseUrl, app);
|
setupRoutes(baseUrl, app);
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Listening on port ${port}`);
|
log.info(`Listening on port ${port}`);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
28
backend/src/logger/logger.controller.ts
Normal file
28
backend/src/logger/logger.controller.ts
Normal 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);
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import sql from "mssql";
|
import sql from "mssql";
|
||||||
import { prodSqlConfig } from "../configs/prodSql.config.js";
|
import { prodSqlConfig } from "../configs/prodSql.config.js";
|
||||||
|
import { createLogger } from "../logger/logger.controller.js";
|
||||||
import { checkHostnamePort } from "../utils/checkHost.utils.js";
|
import { checkHostnamePort } from "../utils/checkHost.utils.js";
|
||||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||||
|
|
||||||
@@ -9,7 +10,10 @@ export let reconnecting = false;
|
|||||||
|
|
||||||
export const connectProdSql = async () => {
|
export const connectProdSql = async () => {
|
||||||
const serverUp = await checkHostnamePort(`${process.env.PROD_SERVER}:1433`);
|
const serverUp = await checkHostnamePort(`${process.env.PROD_SERVER}:1433`);
|
||||||
|
const log = createLogger({
|
||||||
|
module: "system",
|
||||||
|
subModule: "db",
|
||||||
|
});
|
||||||
if (!serverUp) {
|
if (!serverUp) {
|
||||||
// we will try to reconnect
|
// we will try to reconnect
|
||||||
connected = false;
|
connected = false;
|
||||||
@@ -37,7 +41,7 @@ export const connectProdSql = async () => {
|
|||||||
try {
|
try {
|
||||||
pool = await sql.connect(prodSqlConfig);
|
pool = await sql.connect(prodSqlConfig);
|
||||||
connected = true;
|
connected = true;
|
||||||
console.log(
|
log.info(
|
||||||
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
|
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -54,6 +58,10 @@ export const connectProdSql = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const closePool = async () => {
|
export const closePool = async () => {
|
||||||
|
const log = createLogger({
|
||||||
|
module: "system",
|
||||||
|
subModule: "db",
|
||||||
|
});
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
return returnFunc({
|
return returnFunc({
|
||||||
success: false,
|
success: false,
|
||||||
@@ -66,7 +74,7 @@ export const closePool = async () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await pool.close();
|
await pool.close();
|
||||||
console.log("Connection pool closed");
|
log.info("Connection pool closed");
|
||||||
connected = false;
|
connected = false;
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -78,6 +86,10 @@ export const closePool = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
export const reconnectToSql = async () => {
|
export const reconnectToSql = async () => {
|
||||||
|
const log = createLogger({
|
||||||
|
module: "system",
|
||||||
|
subModule: "db",
|
||||||
|
});
|
||||||
if (reconnecting) return;
|
if (reconnecting) return;
|
||||||
|
|
||||||
//set reconnecting to true while we try to reconnect
|
//set reconnecting to true while we try to reconnect
|
||||||
@@ -90,7 +102,7 @@ export const reconnectToSql = async () => {
|
|||||||
|
|
||||||
while (!connected && attempt < maxAttempts) {
|
while (!connected && attempt < maxAttempts) {
|
||||||
attempt++;
|
attempt++;
|
||||||
console.log(
|
log.info(
|
||||||
`Reconnect attempt ${attempt}/${maxAttempts} in ${delayStart / 1000}s ...`,
|
`Reconnect attempt ${attempt}/${maxAttempts} in ${delayStart / 1000}s ...`,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -107,7 +119,7 @@ export const reconnectToSql = async () => {
|
|||||||
pool = await sql.connect(prodSqlConfig);
|
pool = await sql.connect(prodSqlConfig);
|
||||||
reconnecting = false;
|
reconnecting = false;
|
||||||
connected = true;
|
connected = true;
|
||||||
console.log(
|
log.info(
|
||||||
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
|
`${prodSqlConfig.server} is connected to ${prodSqlConfig.database}`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -125,7 +137,8 @@ export const reconnectToSql = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
console.log(
|
log.error(
|
||||||
|
{ notify: true },
|
||||||
"Max reconnect attempts reached on the prodSql server. Stopping retries.",
|
"Max reconnect attempts reached on the prodSql server. Stopping retries.",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type { Express } from "express";
|
|||||||
|
|
||||||
// import the routes and route setups
|
// import the routes and route setups
|
||||||
import { setupApiDocsRoutes } from "./configs/scaler.config.js";
|
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) => {
|
export const setupRoutes = (baseUrl: string, app: Express) => {
|
||||||
//setup all the routes
|
//setup all the routes
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
import { createLogger } from "../logger/logger.controller.js";
|
||||||
|
|
||||||
interface Data {
|
interface Data {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
module: "system" | "ocp";
|
module: "system" | "ocp";
|
||||||
subModule?: "db" | "labeling" | "printer";
|
subModule: "db" | "labeling" | "printer";
|
||||||
level: "info" | "error" | "debug" | "fatal";
|
level: "info" | "error" | "debug" | "fatal";
|
||||||
message: string;
|
message: string;
|
||||||
data?: unknown[];
|
data?: unknown[];
|
||||||
@@ -24,19 +26,20 @@ interface Data {
|
|||||||
*/
|
*/
|
||||||
export const returnFunc = (data: Data) => {
|
export const returnFunc = (data: Data) => {
|
||||||
const notify = data.notify ? data.notify : false;
|
const notify = data.notify ? data.notify : false;
|
||||||
|
const log = createLogger({ module: data.module, subModule: data.subModule });
|
||||||
// handle the logging part
|
// handle the logging part
|
||||||
switch (data.level) {
|
switch (data.level) {
|
||||||
case "info":
|
case "info":
|
||||||
console.log({ notify: notify }, data.message);
|
log.info({ notify: notify }, data.message);
|
||||||
break;
|
break;
|
||||||
case "error":
|
case "error":
|
||||||
console.log({ notify: notify }, data.message);
|
log.error({ notify: notify, error: data.data }, data.message);
|
||||||
break;
|
break;
|
||||||
case "debug":
|
case "debug":
|
||||||
console.log({ notify: notify }, data.message);
|
log.debug({ notify: notify }, data.message);
|
||||||
break;
|
break;
|
||||||
case "fatal":
|
case "fatal":
|
||||||
console.log({ notify: notify }, data.message);
|
log.fatal({ notify: notify }, data.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// api section to return
|
// api section to return
|
||||||
|
|||||||
451
package-lock.json
generated
451
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@@ -30,28 +30,30 @@
|
|||||||
"@changesets/cli": "^2.27.0",
|
"@changesets/cli": "^2.27.0",
|
||||||
"@commitlint/cli": "^18.4.0",
|
"@commitlint/cli": "^18.4.0",
|
||||||
"@commitlint/config-conventional": "^18.4.0",
|
"@commitlint/config-conventional": "^18.4.0",
|
||||||
|
"@scalar/express-api-reference": "^0.8.28",
|
||||||
"@types/express": "^5.0.6",
|
"@types/express": "^5.0.6",
|
||||||
"@types/mssql": "^9.1.8",
|
"@types/mssql": "^9.1.8",
|
||||||
"@types/node": "^24.10.1",
|
"@types/node": "^24.10.1",
|
||||||
"@types/swagger-jsdoc": "^6.0.4",
|
"@types/swagger-jsdoc": "^6.0.4",
|
||||||
"@types/swagger-ui-express": "^4.1.8",
|
"@types/swagger-ui-express": "^4.1.8",
|
||||||
"@vercel/ncc": "^0.38.4",
|
"@vercel/ncc": "^0.38.4",
|
||||||
|
"axios": "^1.13.2",
|
||||||
|
"better-auth": "^1.4.6",
|
||||||
"commitizen": "^4.3.0",
|
"commitizen": "^4.3.0",
|
||||||
"cz-conventional-changelog": "^3.3.0",
|
"cz-conventional-changelog": "^3.3.0",
|
||||||
|
"express": "^5.2.1",
|
||||||
"husky": "^8.0.3",
|
"husky": "^8.0.3",
|
||||||
|
"mssql": "^12.2.0",
|
||||||
|
"npm-check-updates": "^19.1.2",
|
||||||
"openapi-types": "^12.1.3",
|
"openapi-types": "^12.1.3",
|
||||||
|
"pino": "^10.1.0",
|
||||||
|
"pino-pretty": "^13.1.3",
|
||||||
"ts-node-dev": "^2.0.0",
|
"ts-node-dev": "^2.0.0",
|
||||||
"tsx": "^4.21.0",
|
"tsx": "^4.21.0",
|
||||||
"typescript": "^5.9.3"
|
"typescript": "^5.9.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@dotenvx/dotenvx": "^1.51.2",
|
"@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"
|
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"commitizen": {
|
"commitizen": {
|
||||||
|
|||||||
Reference in New Issue
Block a user