add root id in log.ts
This commit is contained in:
parent
03d37425ae
commit
c51b05fa2d
1 changed files with 9 additions and 3 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
import { Client } from "@elastic/elasticsearch";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { PosMaster } from "../entities/PosMaster";
|
||||
|
||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
||||
|
|
@ -23,6 +25,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
if (!req.url.startsWith("/api/")) return next();
|
||||
|
||||
let data: any;
|
||||
const repoPosmaster = AppDataSource.getRepository(PosMaster);
|
||||
|
||||
const originalJson = res.json;
|
||||
|
||||
|
|
@ -30,12 +33,15 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
data = v;
|
||||
return originalJson.call(this, v);
|
||||
};
|
||||
|
||||
const timestamp = new Date().toISOString();
|
||||
const start = performance.now();
|
||||
|
||||
req.app.locals.logData = {};
|
||||
|
||||
const rootId = await repoPosmaster.findOne({
|
||||
where: { current_holderId: req.app.locals.logData.userId },
|
||||
});
|
||||
|
||||
res.on("finish", () => {
|
||||
if (!req.url.startsWith("/api/")) return;
|
||||
let system = "organization";
|
||||
|
|
@ -56,14 +62,14 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
if (req.url.startsWith("/api/v1/org/profile-temp/")) system = "registry";
|
||||
|
||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
||||
|
||||
|
||||
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?rootId.orgRootId:null,
|
||||
systemName: system,
|
||||
startTimeStamp: timestamp,
|
||||
endTimeStamp: new Date().toISOString(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue