add rootId for log
This commit is contained in:
parent
c1f70f6dd0
commit
44212527cb
5 changed files with 101 additions and 5 deletions
|
|
@ -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,10 +45,15 @@ 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,
|
||||
rootId: rootId?rootId.orgRootId:null,
|
||||
systemName: "salary",
|
||||
startTimeStamp: timestamp,
|
||||
endTimeStamp: new Date().toISOString(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue