feat(logger): added pino in and removed all console logs
This commit is contained in:
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 { 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.",
|
||||
);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user