api-helper-kit
Version:
Rohan api helper kit
139 lines (129 loc) • 3.89 kB
JavaScript
const winston = require("winston");
const path = require("path");
const fs = require("fs");
// ! Fetching current time for log ......
const now = new Date();
const formattedDateTime = now.toLocaleString("en-US", {
dateStyle: "medium", // 'full', 'long', 'medium', 'short'
timeStyle: "short", // 'full', 'long', 'medium', 'short'
});
const Main = {
init: () => winston.createLogger({
level: "info",
format: winston.format.json(),
defaultMeta: { timestamp: formattedDateTime },
transports: [
new winston.transports.File({
filename: "logs/error.log",
level: "error",
}),
new winston.transports.File({ filename: "logs/combined.log" }),
],
}),
set : (app) =>{
try {
// ! Logger Routes
app.get("/test-logger", Main.testController);
app.get("/get-combine-log", Main.combineLogController);
app.get("/get-error-log", Main.errorLogController);
app.get("/empty-combine-log", Main.combinedLogEmptyController);
app.get("/empty-error-log", Main.errorLogEmptyController);
} catch (error) {
console.log("Logger Error :",error)
}
},
combineLogController: async (req, res) => {
try {
const rootPath = path.dirname(require.main.filename);
res.sendFile(path.join(rootPath, "logs", "combined.log"));
} catch (error) {
res.status(500).json({
message: `🔴 Internal Server Error :: ${error}`,
error: true,
});
}
},
errorLogController: async (req, res) => {
try {
const rootPath = path.dirname(require.main.filename);
res.sendFile(path.join(rootPath, "logs", "error.log"));
} catch (error) {
res.status(500).json({
message: `🔴 Internal Server Error :: ${error}`,
error: true,
});
}
},
combinedLogEmptyController: async (req, res) => {
try {
const rootPath = path.dirname(require.main.filename);
const combinedFilePath = path.join(rootPath, "logs", "combined.log");
fs.truncate(combinedFilePath, 0, (err) => {
if (err) {
console.error("Error truncating file:", err);
res.status(500).json({
message: `Internal Server Error :: ${err}`,
error: true,
});
} else {
res.status(200).json({
message: `File Truncate(Empty) Successful !`,
error: false,
});
}
});
} catch (error) {
res.status(500).json({
message: `Internal Server Error :: ${error}`,
error: true,
});
}
},
errorLogEmptyController: async (req, res) => {
try {
const rootPath = path.dirname(require.main.filename);
const combinedFilePath = path.join(rootPath, "logs", "error.log");
fs.truncate(combinedFilePath, 0, (err) => {
if (err) {
console.error("Error truncating file:", err);
res.status(500).json({
message: `Internal Server Error :: ${err}`,
error: true,
});
} else {
res.status(200).json({
message: `File Truncate(Empty) Successful !`,
error: false,
});
}
});
} catch (error) {
res.status(500).json({
message: `Internal Server Error :: ${error}`,
error: true,
});
}
},
testController : async (req, res) => {
try {
const log = Main.init()
log.info("🟢 Logger Created");
log.error("🔴 Error Logger Created");
res.status(500).json({
message: `🟢 Logger Created !`,
error: false,
data: {},
});
} catch (error) {
console.log("🚫 SERVER ERROR :: ", error);
res.status(500).json({
message: `🚫 INTERNAL SERVER ERROR :: ${error}`,
error: true,
data: {},
});
}
}
};
module.exports = {
serverLogger: Main,
};