add rootId for log

This commit is contained in:
AdisakKanthawilang 2024-10-25 17:00:48 +07:00
parent a4647e102a
commit 212b9d1466
5 changed files with 100 additions and 6 deletions

View file

@ -1,5 +1,6 @@
import { NextFunction, Request, Response } from "express";
import { Client } from "@elastic/elasticsearch";
import permission from "../interfaces/permission";
if (!process.env.ELASTICSEARCH_INDEX) {
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
@ -36,7 +37,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
req.app.locals.logData = {};
res.on("finish", () => {
res.on("finish", async () => {
if (!req.url.startsWith("/api/")) return;
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
@ -44,7 +45,11 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (level === 1 && res.statusCode < 500) return;
if (level === 2 && res.statusCode < 400) return;
if (level === 3 && res.statusCode < 200) return;
let token: any;
token = req.headers['authorization']
const rootId = await new permission().checkOrg(token,req.app.locals.logData.userId);
const obj = {
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
ip: req.ip,