@tsed/platform-log-middleware 7.0.0-beta.12 → 7.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ var PlatformLogMiddleware_1;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.PlatformLogMiddleware = void 0;
4
5
  const tslib_1 = require("tslib");
@@ -9,15 +10,18 @@ const platform_params_1 = require("@tsed/platform-params");
9
10
  * @middleware
10
11
  * @platform
11
12
  */
12
- let PlatformLogMiddleware = class PlatformLogMiddleware {
13
- constructor() {
14
- if (this.logLevel !== "off") {
15
- this.$onResponse = this.onLogEnd.bind(this);
16
- }
17
- }
13
+ let PlatformLogMiddleware = PlatformLogMiddleware_1 = class PlatformLogMiddleware {
14
+ requestFields;
15
+ logRequest;
16
+ logStart;
17
+ logEnd;
18
+ logLevel;
18
19
  get settings() {
19
20
  return this;
20
21
  }
22
+ $onResponse(ctx) {
23
+ return this.onLogEnd(ctx);
24
+ }
21
25
  /**
22
26
  * Handle the request.
23
27
  */
@@ -30,9 +34,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
30
34
  * @param ctx
31
35
  */
32
36
  onLogStart(ctx) {
33
- const { debug, logRequest, logStart } = this.settings;
37
+ const { logRequest, logLevel, logStart } = this.settings;
38
+ ctx.set(PlatformLogMiddleware_1, true);
34
39
  if (logStart) {
35
- if (debug) {
40
+ if (logLevel === "debug") {
36
41
  ctx.logger.debug({
37
42
  event: "request.start"
38
43
  });
@@ -48,9 +53,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
48
53
  * Called when the `$onResponse` is called by Ts.ED (through Express.end).
49
54
  */
50
55
  onLogEnd(ctx) {
51
- const { debug, logRequest, logEnd } = this.settings;
52
- if (logEnd) {
53
- if (debug) {
56
+ const { logRequest, logEnd, logLevel } = this.settings;
57
+ const started = ctx.get(PlatformLogMiddleware_1);
58
+ if (logEnd && started) {
59
+ if (logLevel === "debug") {
54
60
  ctx.logger.debug({
55
61
  event: "request.end",
56
62
  status: ctx.response.statusCode,
@@ -70,8 +76,16 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
70
76
  * Attach all information that will be necessary to log the request. Attach a new `request.log` object.
71
77
  */
72
78
  configureRequest(ctx) {
73
- ctx.logger.minimalRequestPicker = (obj) => ({ ...this.minimalRequestPicker(ctx), ...obj });
74
- ctx.logger.completeRequestPicker = (obj) => ({ ...this.requestToObject(ctx), ...obj });
79
+ ctx.logger.alterLog((obj, level, withRequest) => {
80
+ switch (level) {
81
+ case "info":
82
+ return { ...this.minimalRequestPicker(ctx), ...obj };
83
+ case "debug":
84
+ return withRequest ? { ...this.requestToObject(ctx), ...obj } : obj;
85
+ default:
86
+ return { ...this.requestToObject(ctx), ...obj };
87
+ }
88
+ });
75
89
  }
76
90
  /**
77
91
  * Return complete request info.
@@ -83,6 +97,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
83
97
  return {
84
98
  method: request.method,
85
99
  url: request.url,
100
+ route: request.route,
86
101
  headers: request.headers,
87
102
  body: request.body,
88
103
  query: request.query,
@@ -104,7 +119,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
104
119
  }
105
120
  };
106
121
  tslib_1.__decorate([
107
- (0, di_1.Constant)("logger.requestFields", ["reqId", "method", "url", "duration"]),
122
+ (0, di_1.Constant)("logger.requestFields", ["reqId", "method", "url", "duration", "route"]),
108
123
  tslib_1.__metadata("design:type", Array)
109
124
  ], PlatformLogMiddleware.prototype, "requestFields", void 0);
110
125
  tslib_1.__decorate([
@@ -123,19 +138,14 @@ tslib_1.__decorate([
123
138
  (0, di_1.Constant)("logger.level"),
124
139
  tslib_1.__metadata("design:type", String)
125
140
  ], PlatformLogMiddleware.prototype, "logLevel", void 0);
126
- tslib_1.__decorate([
127
- (0, di_1.Constant)("debug"),
128
- tslib_1.__metadata("design:type", Boolean)
129
- ], PlatformLogMiddleware.prototype, "debug", void 0);
130
141
  tslib_1.__decorate([
131
142
  tslib_1.__param(0, (0, platform_params_1.Context)()),
132
143
  tslib_1.__metadata("design:type", Function),
133
144
  tslib_1.__metadata("design:paramtypes", [Object]),
134
145
  tslib_1.__metadata("design:returntype", void 0)
135
146
  ], PlatformLogMiddleware.prototype, "use", null);
136
- PlatformLogMiddleware = tslib_1.__decorate([
137
- (0, platform_middlewares_1.Middleware)(),
138
- tslib_1.__metadata("design:paramtypes", [])
147
+ PlatformLogMiddleware = PlatformLogMiddleware_1 = tslib_1.__decorate([
148
+ (0, platform_middlewares_1.Middleware)()
139
149
  ], PlatformLogMiddleware);
140
150
  exports.PlatformLogMiddleware = PlatformLogMiddleware;
141
151
  //# sourceMappingURL=PlatformLogMiddleware.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;;;AAAA,iCAAkC;AAClC,qEAAyE;AACzE,2DAA8C;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;IAqBhC;QACE,IAAI,IAAI,CAAC,QAAQ,KAAK,KAAK,EAAE;YAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAC7C;IACH,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,GAAG,CAAY,GAAY;QAChC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;IAED;;;OAGG;IACO,UAAU,CAAC,GAAY;QAC/B,MAAM,EAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAEpD,IAAI,QAAQ,EAAE;YACZ,IAAI,KAAK,EAAE;gBACT,GAAG,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ed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\n\n get settings() {\n return this;\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (debug) {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (debug) {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;;;;AAAA,iCAAkC;AAClC,qEAAyE;AACzE,2DAA8C;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,6BAAlC,MAAa,qBAAqB;IAEtB,aAAa,CAAsB;IAGnC,UAAU,CAAU;IAGpB,QAAQ,CAAU;IAGlB,MAAM,CAAU;IAGhB,QAAQ,CAAS;IAE3B,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC;IACd,CAAC;IAED,WAAW,CAAC,GAAY;QACtB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IAED;;OAEG;IACI,GAAG,CAAY,GAAY;QAChC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;IAED;;;OAGG;IACO,UAAU,CAAC,GAAY;QAC/B,MAAM,EAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAEvD,GAAG,CAAC,GAAG,CAAC,uBAAqB,EAAE,IAAI,CAAC,CAAC;QAErC,IAAI,QAAQ,EAA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AA,aAAQ,EAAC,iBAAiB,EAAE,IAAI,CAAC;;uDACN;AAG5B;IADC,IAAA,aAAQ,EAAC,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,IAAA,aAAQ,EAAC,cAAc,CAAC;;uDACE;AAa3B;IAAY,mBAAA,IAAA,yBAAO,GAAE,CAAA;;;;gDAGpB;AA9BU,qBAAqB;IADjC,IAAA,iCAAU,GAAE;GACA,qBAAqB,CAiIjC;AAjIY,sDAAqB","sourcesContent":["import {Constant} from \"@tsed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\", \"route\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n get settings() {\n return this;\n }\n\n $onResponse(ctx: Context) {\n return this.onLogEnd(ctx);\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {logRequest, logLevel, logStart} = this.settings;\n\n ctx.set(PlatformLogMiddleware, true);\n\n if (logStart) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n protected onLogEnd(ctx: Context) {\n const {logRequest, logEnd, logLevel} = this.settings;\n const started = ctx.get(PlatformLogMiddleware);\n\n if (logEnd && started) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.alterLog((obj: any, level, withRequest) => {\n switch (level) {\n case \"info\":\n return {...this.minimalRequestPicker(ctx), ...obj};\n case \"debug\":\n return withRequest ? {...this.requestToObject(ctx), ...obj} : obj;\n default:\n return {...this.requestToObject(ctx), ...obj};\n }\n });\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n route: request.route,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
@@ -1,3 +1,4 @@
1
+ var PlatformLogMiddleware_1;
1
2
  import { __decorate, __metadata, __param } from "tslib";
2
3
  import { Constant } from "@tsed/di";
3
4
  import { Middleware } from "@tsed/platform-middlewares";
@@ -6,15 +7,18 @@ import { Context } from "@tsed/platform-params";
6
7
  * @middleware
7
8
  * @platform
8
9
  */
9
- let PlatformLogMiddleware = class PlatformLogMiddleware {
10
- constructor() {
11
- if (this.logLevel !== "off") {
12
- this.$onResponse = this.onLogEnd.bind(this);
13
- }
14
- }
10
+ let PlatformLogMiddleware = PlatformLogMiddleware_1 = class PlatformLogMiddleware {
11
+ requestFields;
12
+ logRequest;
13
+ logStart;
14
+ logEnd;
15
+ logLevel;
15
16
  get settings() {
16
17
  return this;
17
18
  }
19
+ $onResponse(ctx) {
20
+ return this.onLogEnd(ctx);
21
+ }
18
22
  /**
19
23
  * Handle the request.
20
24
  */
@@ -27,9 +31,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
27
31
  * @param ctx
28
32
  */
29
33
  onLogStart(ctx) {
30
- const { debug, logRequest, logStart } = this.settings;
34
+ const { logRequest, logLevel, logStart } = this.settings;
35
+ ctx.set(PlatformLogMiddleware_1, true);
31
36
  if (logStart) {
32
- if (debug) {
37
+ if (logLevel === "debug") {
33
38
  ctx.logger.debug({
34
39
  event: "request.start"
35
40
  });
@@ -45,9 +50,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
45
50
  * Called when the `$onResponse` is called by Ts.ED (through Express.end).
46
51
  */
47
52
  onLogEnd(ctx) {
48
- const { debug, logRequest, logEnd } = this.settings;
49
- if (logEnd) {
50
- if (debug) {
53
+ const { logRequest, logEnd, logLevel } = this.settings;
54
+ const started = ctx.get(PlatformLogMiddleware_1);
55
+ if (logEnd && started) {
56
+ if (logLevel === "debug") {
51
57
  ctx.logger.debug({
52
58
  event: "request.end",
53
59
  status: ctx.response.statusCode,
@@ -67,8 +73,16 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
67
73
  * Attach all information that will be necessary to log the request. Attach a new `request.log` object.
68
74
  */
69
75
  configureRequest(ctx) {
70
- ctx.logger.minimalRequestPicker = (obj) => ({ ...this.minimalRequestPicker(ctx), ...obj });
71
- ctx.logger.completeRequestPicker = (obj) => ({ ...this.requestToObject(ctx), ...obj });
76
+ ctx.logger.alterLog((obj, level, withRequest) => {
77
+ switch (level) {
78
+ case "info":
79
+ return { ...this.minimalRequestPicker(ctx), ...obj };
80
+ case "debug":
81
+ return withRequest ? { ...this.requestToObject(ctx), ...obj } : obj;
82
+ default:
83
+ return { ...this.requestToObject(ctx), ...obj };
84
+ }
85
+ });
72
86
  }
73
87
  /**
74
88
  * Return complete request info.
@@ -80,6 +94,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
80
94
  return {
81
95
  method: request.method,
82
96
  url: request.url,
97
+ route: request.route,
83
98
  headers: request.headers,
84
99
  body: request.body,
85
100
  query: request.query,
@@ -101,7 +116,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
101
116
  }
102
117
  };
103
118
  __decorate([
104
- Constant("logger.requestFields", ["reqId", "method", "url", "duration"]),
119
+ Constant("logger.requestFields", ["reqId", "method", "url", "duration", "route"]),
105
120
  __metadata("design:type", Array)
106
121
  ], PlatformLogMiddleware.prototype, "requestFields", void 0);
107
122
  __decorate([
@@ -120,19 +135,14 @@ __decorate([
120
135
  Constant("logger.level"),
121
136
  __metadata("design:type", String)
122
137
  ], PlatformLogMiddleware.prototype, "logLevel", void 0);
123
- __decorate([
124
- Constant("debug"),
125
- __metadata("design:type", Boolean)
126
- ], PlatformLogMiddleware.prototype, "debug", void 0);
127
138
  __decorate([
128
139
  __param(0, Context()),
129
140
  __metadata("design:type", Function),
130
141
  __metadata("design:paramtypes", [Object]),
131
142
  __metadata("design:returntype", void 0)
132
143
  ], PlatformLogMiddleware.prototype, "use", null);
133
- PlatformLogMiddleware = __decorate([
134
- Middleware(),
135
- __metadata("design:paramtypes", [])
144
+ PlatformLogMiddleware = PlatformLogMiddleware_1 = __decorate([
145
+ Middleware()
136
146
  ], PlatformLogMiddleware);
137
147
  export { PlatformLogMiddleware };
138
148
  //# sourceMappingURL=PlatformLogMiddleware.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,UAAU,CAAC;AAClC,OAAO,EAAC,UAAU,EAAoB,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAG9C;;;GAGG;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T,CAAC;CACF,CAAA;AAxHC;IADC,QAAQ,CAAC,sBAAsB,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;;4DAC5B;AAG7C;IADC,QAAQ,CAAC,mBAAmB,EAAE,IAAI,CAAC;;yDACN;AAG9B;IADC,QAAQ,CAAC,iBAAiB,EAAE,IAAI,CAAC;;uDACN;AAG5B;IADC,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,QAAQ,CAAC,cAAc,CAAC;;uDACE;AAG3B;IADC,QAAQ,CAAC,OAAO,CAAC;;oDACO;AAezB;IAAY,WAAA,OAAO,EAAE,CAAA;;;;gDAGpB;AArCU,qBAAqB;IADjC,UAAU,EAAE;;GACA,qBAAqB,CA4HjC;SA5HY,qBAAqB","sourcesContent":["import {Constant} from \"@tsed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\n\n get settings() {\n return this;\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (debug) {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (debug) {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,UAAU,CAAC;AAClC,OAAO,EAAC,UAAU,EAAoB,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAG9C;;;GAGG;AAEH,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pB,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACO,oBAAoB,CAAC,GAAY;QACzC,MAAM,EAAC,aAAa,EAAC,GAAG,IAAI,CAAC;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;QAEvC,OAAO,aAAc,CAAC,MAAM,CAAC,CAAC,GAAQ,EAAE,GAAW,EAAE,EAAE;YACrD,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;YAErB,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF,CAAA;AA/HC;IADC,QAAQ,CAAC,sBAAsB,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;;4DACrC;AAG7C;IADC,QAAQ,CAAC,mBAAmB,EAAE,IAAI,CAAC;;yDACN;AAG9B;IADC,QAAQ,CAAC,iBAAiB,EAAE,IAAI,CAAC;;uDACN;AAG5B;IADC,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,QAAQ,CAAC,cAAc,CAAC;;uDACE;AAa3B;IAAY,WAAA,OAAO,EAAE,CAAA;;;;gDAGpB;AA9BU,qBAAqB;IADjC,UAAU,EAAE;GACA,qBAAqB,CAiIjC;SAjIY,qBAAqB","sourcesContent":["import {Constant} from \"@tsed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\", \"route\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n get settings() {\n return this;\n }\n\n $onResponse(ctx: Context) {\n return this.onLogEnd(ctx);\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {logRequest, logLevel, logStart} = this.settings;\n\n ctx.set(PlatformLogMiddleware, true);\n\n if (logStart) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n protected onLogEnd(ctx: Context) {\n const {logRequest, logEnd, logLevel} = this.settings;\n const started = ctx.get(PlatformLogMiddleware);\n\n if (logEnd && started) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.alterLog((obj: any, level, withRequest) => {\n switch (level) {\n case \"info\":\n return {...this.minimalRequestPicker(ctx), ...obj};\n case \"debug\":\n return withRequest ? {...this.requestToObject(ctx), ...obj} : obj;\n default:\n return {...this.requestToObject(ctx), ...obj};\n }\n });\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n route: request.route,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
@@ -6,15 +6,13 @@ import type { LoggerRequestFields } from "../domain/PlatformLogMiddlewareSetting
6
6
  * @platform
7
7
  */
8
8
  export declare class PlatformLogMiddleware implements MiddlewareMethods {
9
- $onResponse: any;
10
9
  protected requestFields: LoggerRequestFields;
11
10
  protected logRequest: boolean;
12
11
  protected logStart: boolean;
13
12
  protected logEnd: boolean;
14
13
  protected logLevel: string;
15
- protected debug: boolean;
16
- constructor();
17
14
  get settings(): this;
15
+ $onResponse(ctx: Context): void;
18
16
  /**
19
17
  * Handle the request.
20
18
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/platform-log-middleware",
3
- "version": "7.0.0-beta.12",
3
+ "version": "7.0.0-beta.15",
4
4
  "description": "Log request middleware module for Ts.ED Framework",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -21,17 +21,17 @@
21
21
  "test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
22
22
  },
23
23
  "dependencies": {
24
- "tslib": "2.3.1"
24
+ "tslib": "2.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@tsed/di": "7.0.0-beta.12",
28
- "@tsed/platform-middlewares": "7.0.0-beta.12",
29
- "@tsed/platform-params": "7.0.0-beta.12"
27
+ "@tsed/di": "7.0.0-beta.15",
28
+ "@tsed/platform-middlewares": "7.0.0-beta.15",
29
+ "@tsed/platform-params": "7.0.0-beta.15"
30
30
  },
31
31
  "peerDependencies": {
32
- "@tsed/di": "^7.0.0-beta.12",
33
- "@tsed/platform-middlewares": "^7.0.0-beta.12",
34
- "@tsed/platform-params": "^7.0.0-beta.12"
32
+ "@tsed/di": "^7.0.0-beta.15",
33
+ "@tsed/platform-middlewares": "^7.0.0-beta.15",
34
+ "@tsed/platform-params": "^7.0.0-beta.15"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@tsed/di": {
package/readme.md CHANGED
@@ -56,7 +56,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
56
56
 
57
57
  The MIT License (MIT)
58
58
 
59
- Copyright (c) 2016 - 2018 Romain Lenzotti
59
+ Copyright (c) 2016 - 2022 Romain Lenzotti
60
60
 
61
61
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
62
62