"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.errLogger = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const uuid_1 = require("uuid"); function errLogger(error, req, res, next) { const dataLog = `${req.method}\t${(0, uuid_1.v4)()}\t${req.ip}\t${req.originalUrl}\t${Date.now()}\t${new Date()}\t${error}\t${JSON.stringify(req.body)}`; const pathFolderLog = path_1.default.join(__dirname, "../log"); const filePath = `${pathFolderLog}/error/log_${getCurrentDate()}.txt`; if (fs_1.default.existsSync(pathFolderLog) === true) { if (fs_1.default.existsSync(`${pathFolderLog}/error`) === true) { if (fs_1.default.existsSync(filePath) === true) { fs_1.default.appendFileSync(filePath, `\n${dataLog}`); } else { fs_1.default.writeFileSync(filePath, dataLog); } } else { fs_1.default.mkdirSync(`${pathFolderLog}/error`, { recursive: true }); fs_1.default.writeFileSync(filePath, dataLog); } } else { fs_1.default.mkdirSync(`${pathFolderLog}/error`, { recursive: true }); fs_1.default.writeFileSync(filePath, dataLog); } res.status(error.status || 500).json({ metadata: { msg: error.msg || "Internal Server Error", code: error.code || 500, }, }); } exports.errLogger = errLogger; function getCurrentDate() { const currentDate = new Date(); const year = currentDate.getUTCFullYear(); let month = currentDate.getUTCMonth() + 1; month = month < 10 ? "0" + month : month; let day = currentDate.getUTCDate(); day = day < 10 ? "0" + day : day; const formattedDate = `${year}-${month}-${day}`; return formattedDate; }