fix
This commit is contained in:
parent
5edd511f2a
commit
4ed904316d
4 changed files with 21 additions and 27 deletions
|
|
@ -4,18 +4,29 @@ import { DataSource, LogLevel, LogMessage } from "typeorm";
|
||||||
import { Logger } from "typeorm";
|
import { Logger } from "typeorm";
|
||||||
import { QueryRunner } from "typeorm/browser";
|
import { QueryRunner } from "typeorm/browser";
|
||||||
import { RequestWithUser } from "../middlewares/user";
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
|
import { addLogSequence } from "../interfaces/utils";
|
||||||
|
|
||||||
export class MyCustomLogger implements Logger {
|
export class MyCustomLogger implements Logger {
|
||||||
log(level: "log" | "info" | "warn", message: any, queryRunner?: QueryRunner) {}
|
log(level: "log" | "info" | "warn", message: any, queryRunner?: QueryRunner) {}
|
||||||
|
|
||||||
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): void {
|
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): void {
|
||||||
const req = queryRunner?.data as RequestWithUser | undefined;
|
const req = queryRunner?.data as RequestWithUser;
|
||||||
const logData = req?.app?.locals.logData?.sequence?.at(-1);
|
if (req?.app?.locals.logData?.sequence) {
|
||||||
|
addLogSequence(req, {
|
||||||
|
action: "database",
|
||||||
|
status: "success",
|
||||||
|
description: "Query Data.",
|
||||||
|
query: "Query: " + query + (parameters ? " - Parameters:" + JSON.stringify(parameters) : ""),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// const req = queryRunner?.data as RequestWithUser | undefined;
|
||||||
|
// const logData = req?.app?.locals.logData?.sequence?.at(-1);
|
||||||
|
|
||||||
if (logData && !logData.query) logData.query = [];
|
// if (logData && !logData.query) logData.query = [];
|
||||||
if (logData) logData.query.push(
|
// if (logData) logData.query.push(
|
||||||
"Query: " + query + (parameters ? (" - Parameters:" + JSON.stringify(parameters)) : '')
|
// "Query: " + query + (parameters ? (" - Parameters:" + JSON.stringify(parameters)) : '')
|
||||||
);
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
logMigration(message: string, queryRunner?: QueryRunner) {}
|
logMigration(message: string, queryRunner?: QueryRunner) {}
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,4 @@ class HttpSuccess {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// const request = {} as RequestWithUser;
|
|
||||||
// if (request) {
|
|
||||||
// if (!request.app.locals.logData) {
|
|
||||||
// request.app.locals.logData = {};
|
|
||||||
// }
|
|
||||||
// addLogSequence(request, {
|
|
||||||
// action: "database",
|
|
||||||
// status: "success",
|
|
||||||
// description: "Query Data.",
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
export default HttpSuccess;
|
export default HttpSuccess;
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ export function setLogDataDiff(req: RequestWithUser, data: DataDiff) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addLogSequence(req: RequestWithUser, data: LogSequence) {
|
export function addLogSequence(req: RequestWithUser, data: LogSequence) {
|
||||||
if (!req.app.locals.logData.sequence) {
|
if (!req?.app?.locals?.logData?.sequence) {
|
||||||
req.app.locals.logData.sequence = [];
|
req.app.locals.logData.sequence = [];
|
||||||
}
|
}
|
||||||
req.app.locals.logData.sequence = req.app.locals.logData.sequence.concat(data);
|
req.app.locals.logData.sequence = req.app.locals.logData.sequence.concat(data);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import { Client } from "@elastic/elasticsearch";
|
import { Client } from "@elastic/elasticsearch";
|
||||||
import { addLogSequence } from "../interfaces/utils";
|
|
||||||
import { RequestWithUser } from "./user";
|
|
||||||
|
|
||||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||||
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
||||||
|
|
@ -21,7 +19,7 @@ const elasticsearch = new Client({
|
||||||
node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`,
|
node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
async function logMiddleware(req: any, res: Response, next: NextFunction) {
|
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
if (!req.url.startsWith("/api/")) return next();
|
if (!req.url.startsWith("/api/")) return next();
|
||||||
|
|
||||||
let data: any;
|
let data: any;
|
||||||
|
|
@ -40,13 +38,8 @@ async function logMiddleware(req: any, res: Response, next: NextFunction) {
|
||||||
|
|
||||||
res.on("finish", () => {
|
res.on("finish", () => {
|
||||||
if (!req.url.startsWith("/api/")) return;
|
if (!req.url.startsWith("/api/")) return;
|
||||||
const logData = req?.app?.locals.logData?.sequence?.at(-1);
|
|
||||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
||||||
addLogSequence(req, {
|
|
||||||
action: "database",
|
|
||||||
status: "success",
|
|
||||||
description: "Query Data.",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (level === 1 && res.statusCode < 500) return;
|
if (level === 1 && res.statusCode < 500) return;
|
||||||
if (level === 2 && res.statusCode < 400) return;
|
if (level === 2 && res.statusCode < 400) return;
|
||||||
|
|
@ -74,6 +67,7 @@ async function logMiddleware(req: any, res: Response, next: NextFunction) {
|
||||||
document: obj,
|
document: obj,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue