-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
PlatformLogMiddleware.ts
158 lines (139 loc) · 4.06 KB
/
PlatformLogMiddleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {cleanObject} from "@tsed/core";
import {Constant} from "@tsed/di";
import {Middleware, MiddlewareMethods} from "@tsed/platform-middlewares";
import {Context} from "@tsed/platform-params";
import type {LoggerRequestFields} from "../domain/PlatformLogMiddlewareSettings";
/**
* @middleware
* @platform
*/
@Middleware()
export class PlatformLogMiddleware implements MiddlewareMethods {
@Constant("logger.requestFields", ["reqId", "method", "url", "duration", "route"])
protected requestFields: LoggerRequestFields;
@Constant("logger.logRequest", true)
protected logRequest: boolean;
@Constant("logger.logStart", true)
protected logStart: boolean;
@Constant("logger.logEnd", true)
protected logEnd: boolean;
@Constant("logger.level")
protected logLevel: string;
get settings() {
return this;
}
/**
* Handle the request.
*/
public use(@Context() ctx: Context): void {
this.configureRequest(ctx);
this.onLogStart(ctx);
ctx.response.onEnd(() => this.onLogEnd(ctx));
}
/**
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
*/
onLogEnd(ctx: Context) {
const {logRequest, logEnd, logLevel} = this.settings;
const started = ctx.logStarted;
if (logEnd && started) {
if (ctx.response.statusCode >= 400) {
ctx.logger.error({
event: "request.end",
status: ctx.response.statusCode,
status_code: String(ctx.response.statusCode),
state: "KO",
...cleanObject({
error_name: ctx.error?.name,
error_message: ctx.error?.message,
error_errors: ctx.error?.errors,
error_stack: ctx.error?.stack,
error_body: ctx.error?.body,
error_headers: ctx.error?.headers
})
});
} else {
if (logLevel === "debug") {
ctx.logger.debug({
event: "request.end",
status: ctx.response.statusCode,
status_code: String(ctx.response.statusCode),
data: ctx.data,
state: "OK"
});
} else if (logRequest) {
ctx.logger.info({
event: "request.end",
status: ctx.response.statusCode,
status_code: String(ctx.response.statusCode),
state: "OK"
});
}
}
}
}
/**
* The separate onLogStart() function will allow developer to overwrite the initial request log.
* @param ctx
*/
protected onLogStart(ctx: Context) {
const {logRequest, logLevel, logStart} = this.settings;
ctx.logStarted = true;
if (logStart) {
if (logLevel === "debug") {
ctx.logger.debug({
event: "request.start"
});
} else if (logRequest) {
ctx.logger.info({
event: "request.start"
});
}
}
}
/**
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
*/
protected configureRequest(ctx: Context) {
ctx.logger.alterLog((obj: any, level, withRequest) => {
switch (level) {
case "info":
return {...this.minimalRequestPicker(ctx), ...obj};
case "debug":
return withRequest ? {...this.requestToObject(ctx), ...obj} : obj;
default:
return {...this.requestToObject(ctx), ...obj};
}
});
}
/**
* Return complete request info.
* @returns {Object}
* @param ctx
*/
protected requestToObject(ctx: Context): any {
const {request} = ctx;
return {
method: request.method,
url: request.url,
route: request.route || request.url,
headers: request.headers,
body: request.body,
query: request.query,
params: request.params
};
}
/**
* Return a filtered request from global configuration.
* @returns {Object}
* @param ctx
*/
protected minimalRequestPicker(ctx: Context): any {
const {requestFields} = this;
const info = this.requestToObject(ctx);
return requestFields!.reduce((acc: any, key: string) => {
acc[key] = info[key];
return acc;
}, {});
}
}