feat: logging data structure and conditioan level
This commit is contained in:
parent
41c4d5e8f2
commit
9c176b9878
1 changed files with 17 additions and 2 deletions
|
|
@ -7,6 +7,14 @@ if (!process.env.ELASTICSEARCH_INDEX) {
|
|||
|
||||
const ELASTICSEARCH_INDEX = process.env.ELASTICSEARCH_INDEX;
|
||||
|
||||
const LOG_LEVEL_MAP: Record<string, number> = {
|
||||
debug: 4,
|
||||
info: 3,
|
||||
warning: 2,
|
||||
error: 1,
|
||||
none: 0,
|
||||
};
|
||||
|
||||
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
if (!req.url.startsWith("/api")) return next();
|
||||
|
||||
|
|
@ -25,7 +33,14 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
req.app.locals.logData = {};
|
||||
|
||||
res.on("finish", () => {
|
||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "info"] || 1;
|
||||
|
||||
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",
|
||||
systemName: "JWS-SOS",
|
||||
startTimeStamp: timestamp,
|
||||
endTimeStamp: new Date().toString(),
|
||||
|
|
@ -43,8 +58,8 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
: { 200: "success", 201: "created_success", 204: "no_content", 304: "success" }[
|
||||
res.statusCode
|
||||
],
|
||||
input: req.body,
|
||||
output: data,
|
||||
// input: (level < 1 && req.body) || undefined,
|
||||
// output: (level < 1 && data) || undefined,
|
||||
...req.app.locals.logData,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue