2025-04-27 23:29:32 +07:00

45 lines
1.8 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const uuid_1 = require("uuid");
function logger(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${JSON.stringify(req.body)}`;
const pathFolderLog = path_1.default.join(__dirname, "../log");
const filePath = `${pathFolderLog}/access/log_${getCurrentDate()}.txt`;
if (fs_1.default.existsSync(pathFolderLog) === true) {
if (fs_1.default.existsSync(`${pathFolderLog}/access`) === 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}/access`, { recursive: true });
fs_1.default.writeFileSync(filePath, dataLog);
}
}
else {
fs_1.default.mkdirSync(`${pathFolderLog}/access`, { recursive: true });
fs_1.default.writeFileSync(filePath, dataLog);
}
next();
}
exports.logger = logger;
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;
}