hrms-api-org/src/middlewares/logs.ts

114 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-08-09 10:17:39 +07:00
import { NextFunction, Request, Response } from "express";
import { Client } from "@elastic/elasticsearch";
import { logMemoryStore } from "../utils/log-memory-store";
2024-08-09 10:17:39 +07:00
if (!process.env.ELASTICSEARCH_INDEX) {
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
}
const ELASTICSEARCH_INDEX = process.env.ELASTICSEARCH_INDEX;
const LOG_LEVEL_MAP: Record<string, number> = {
debug: 4,
info: 3,
warning: 2,
error: 1,
none: 0,
};
const elasticsearch = new Client({
node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`,
});
2024-12-20 10:03:23 +07:00
2024-08-09 10:17:39 +07:00
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (!req.url.startsWith("/api/")) return next();
let data: any;
2024-12-20 10:03:23 +07:00
2024-08-09 10:17:39 +07:00
const originalJson = res.json;
res.json = function (v: any) {
data = v;
return originalJson.call(this, v);
};
const timestamp = new Date().toISOString();
const start = performance.now();
req.app.locals.logData = {};
2024-12-18 18:59:06 +07:00
res.on("finish", async () => {
try {
if (!req.url.startsWith("/api/")) return;
2024-12-20 10:03:23 +07:00
let system = "organization";
if (req.url.startsWith("/api/v1/org/keycloak/log/sso")) system = "inout";
if (req.url.startsWith("/api/v1/org/metadata/")) system = "master";
if (req.url.startsWith("/api/v1/org/pos/position/")) system = "master";
if (req.url.startsWith("/api/v1/org/pos/type/")) system = "master";
if (req.url.startsWith("/api/v1/org/employee/pos/position/")) system = "master";
if (req.url.startsWith("/api/v1/org/employee/pos/type/")) system = "master";
if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin";
if (req.url.startsWith("/api/v1/org/auth/authRole/")) system = "admin";
// if (req.url.startsWith("/api/v1/org/keycloak")) system = "admin";
if (req.url.startsWith("/api/v1/org/pos/admin/master/list")) system = "admin";
if (req.url.startsWith("/api/v1/org/super-admin/{id}")) system = "admin";
if (req.url.startsWith("/api/v1/org/permission-org/")) system = "admin";
if (req.url.startsWith("/api/v1/org/pos/assign/")) system = "admin";
if (req.url.startsWith("/api/v1/org/profile/")) system = "registry";
if (req.url.startsWith("/api/v1/org/profile-employee/")) system = "registry";
if (req.url.startsWith("/api/v1/org/profile-temp/")) system = "registry";
2025-10-22 15:43:18 +07:00
if (req.url.startsWith("/api/v1/org/commandType/admin")) system = "admin";
if (req.url.startsWith("/api/v1/org/commandSys/")) system = "admin";
if (req.url.startsWith("/api/v1/org/commandSalary/")) system = "admin";
if (req.url.startsWith("/api/v1/org/apiKey/")) system = "admin";
if (req.url.startsWith("/api/v1/org/api-manage/")) system = "admin";
2024-12-18 18:59:06 +07:00
2025-12-18 17:29:18 +07:00
if (req.url.startsWith("/api/v1/org/keycloak/")) system = "registry";
2024-12-18 18:59:06 +07:00
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
// Get profile from cache
const profileByKeycloak = await logMemoryStore.getProfileByKeycloak(
req.app.locals.logData.userId,
);
// Get rootId from cache
const rootId = await logMemoryStore.getRootIdByProfileId(profileByKeycloak?.id);
// console.log("ancestorDNA:", rootId);
2024-12-18 18:59:06 +07:00
if (level === 1 && res.statusCode < 500) return;
if (level === 2 && res.statusCode < 400) return;
if (level === 3 && res.statusCode < 200) return;
const obj = {
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
ip: req.ip,
rootId: rootId ?? null,
2024-12-20 10:03:23 +07:00
systemName: system,
2024-12-18 18:59:06 +07:00
startTimeStamp: timestamp,
endTimeStamp: new Date().toISOString(),
processTime: performance.now() - start,
host: req.hostname,
method: req.method,
endpoint: req.url,
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
responseDescription: data?.message,
input: level === 4 ? JSON.stringify(req.body, null, 2) : undefined,
output: level === 4 ? JSON.stringify(data, null, 2) : undefined,
...req.app.locals.logData,
};
// Send log to Elasticsearch
await elasticsearch.index({
index: ELASTICSEARCH_INDEX,
document: obj,
});
} catch (err) {
console.error("Error in logMiddleware:", err);
}
2024-08-09 10:17:39 +07:00
});
return next();
}
export default logMiddleware;