infront-logger 1.0.9 → 1.1.0
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.
- package/dist/index.es.js +142 -22
- package/dist/index.umd.js +142 -22
- package/dist/main.d.ts +17 -3
- package/package.json +2 -1
package/dist/index.es.js
CHANGED
|
@@ -138,16 +138,16 @@ class Logger {
|
|
|
138
138
|
levels: this.options.levels,
|
|
139
139
|
exitOnError: false,
|
|
140
140
|
// format,
|
|
141
|
-
transports: trans
|
|
142
|
-
exceptionHandlers
|
|
141
|
+
transports: trans
|
|
142
|
+
// exceptionHandlers
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
-
const redact$
|
|
146
|
+
const redact$2 = (data, ...sensitiveKeysList) => {
|
|
147
147
|
var _a, _b;
|
|
148
148
|
if (typeof data === "object" && data !== null && !((_b = (_a = data == null ? void 0 : data.constructor) == null ? void 0 : _a.name) == null ? void 0 : _b.startsWith("model"))) {
|
|
149
149
|
if (Array.isArray(data)) {
|
|
150
|
-
return data.map((item) => redact$
|
|
150
|
+
return data.map((item) => redact$2(item, ...sensitiveKeysList));
|
|
151
151
|
}
|
|
152
152
|
const redactedData = {};
|
|
153
153
|
for (const key in data) {
|
|
@@ -155,7 +155,7 @@ const redact$1 = (data, ...sensitiveKeysList) => {
|
|
|
155
155
|
if (sensitiveKeysList.includes(key)) {
|
|
156
156
|
redactedData[key] = "*****";
|
|
157
157
|
} else {
|
|
158
|
-
redactedData[key] = redact$
|
|
158
|
+
redactedData[key] = redact$2(data[key], ...sensitiveKeysList);
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
}
|
|
@@ -164,7 +164,7 @@ const redact$1 = (data, ...sensitiveKeysList) => {
|
|
|
164
164
|
return data;
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
|
-
const trim$
|
|
167
|
+
const trim$2 = (data, length) => {
|
|
168
168
|
try {
|
|
169
169
|
let str = JSON.stringify(data);
|
|
170
170
|
if (str.length > length) {
|
|
@@ -177,8 +177,8 @@ const trim$1 = (data, length) => {
|
|
|
177
177
|
};
|
|
178
178
|
const http = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
179
179
|
__proto__: null,
|
|
180
|
-
redact: redact$
|
|
181
|
-
trim: trim$
|
|
180
|
+
redact: redact$2,
|
|
181
|
+
trim: trim$2
|
|
182
182
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
183
183
|
function toFixedNumber(num, digits, base) {
|
|
184
184
|
const pow = Math.pow(base ?? 10, digits);
|
|
@@ -187,7 +187,7 @@ function toFixedNumber(num, digits, base) {
|
|
|
187
187
|
function bytesToMB$1(b) {
|
|
188
188
|
return toFixedNumber(b / 1024 / 1024, 2, 10);
|
|
189
189
|
}
|
|
190
|
-
function formatBytes$
|
|
190
|
+
function formatBytes$2(bytes, decimals = 2) {
|
|
191
191
|
if (!+bytes)
|
|
192
192
|
return "0 Bytes";
|
|
193
193
|
const k = 1024;
|
|
@@ -199,13 +199,13 @@ function formatBytes$1(bytes, decimals = 2) {
|
|
|
199
199
|
const format = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
200
200
|
__proto__: null,
|
|
201
201
|
bytesToMB: bytesToMB$1,
|
|
202
|
-
formatBytes: formatBytes$
|
|
202
|
+
formatBytes: formatBytes$2,
|
|
203
203
|
toFixedNumber
|
|
204
204
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
205
205
|
const pick = (obj, ...keys) => Object.fromEntries(
|
|
206
206
|
keys.filter((key) => key in obj).map((key) => [key, obj[key]])
|
|
207
207
|
);
|
|
208
|
-
const omit$
|
|
208
|
+
const omit$2 = (obj, ...keys) => {
|
|
209
209
|
const result = { ...obj };
|
|
210
210
|
keys.forEach(function(prop) {
|
|
211
211
|
delete result[prop];
|
|
@@ -214,7 +214,7 @@ const omit$1 = (obj, ...keys) => {
|
|
|
214
214
|
};
|
|
215
215
|
const object = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
216
216
|
__proto__: null,
|
|
217
|
-
omit: omit$
|
|
217
|
+
omit: omit$2,
|
|
218
218
|
pick
|
|
219
219
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
220
220
|
const v8 = require("v8");
|
|
@@ -302,10 +302,10 @@ class BaseLogger {
|
|
|
302
302
|
return this;
|
|
303
303
|
}
|
|
304
304
|
}
|
|
305
|
-
const { redact, trim } = http;
|
|
306
|
-
const { formatBytes } = format;
|
|
307
|
-
const { omit } = object;
|
|
308
|
-
const MAX_BODY_LENGTH = 128;
|
|
305
|
+
const { redact: redact$1, trim: trim$1 } = http;
|
|
306
|
+
const { formatBytes: formatBytes$1 } = format;
|
|
307
|
+
const { omit: omit$1 } = object;
|
|
308
|
+
const MAX_BODY_LENGTH$1 = 128;
|
|
309
309
|
class HTTPLogger extends BaseLogger {
|
|
310
310
|
constructor(startTime, options = {}) {
|
|
311
311
|
super("http", options);
|
|
@@ -330,20 +330,20 @@ class HTTPLogger extends BaseLogger {
|
|
|
330
330
|
let res = this.res;
|
|
331
331
|
let body = this.data;
|
|
332
332
|
this.ctx.request = {
|
|
333
|
-
headers: redact(req.headers, "cookie"),
|
|
333
|
+
headers: redact$1(req.headers, "cookie"),
|
|
334
334
|
host: req.headers.host,
|
|
335
335
|
baseUrl: req.baseUrl,
|
|
336
336
|
url: req.originalUrl || req.url,
|
|
337
337
|
method: req.method,
|
|
338
|
-
body: redact(req.body, "password"),
|
|
338
|
+
body: redact$1(req.body, "password"),
|
|
339
339
|
params: req == null ? void 0 : req.params,
|
|
340
|
-
query: redact(req == null ? void 0 : req.query, "password"),
|
|
340
|
+
query: redact$1(req == null ? void 0 : req.query, "password"),
|
|
341
341
|
clientIP: ((_a = req == null ? void 0 : req.headers["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? (req == null ? void 0 : req.socket.remoteAddress)
|
|
342
342
|
};
|
|
343
343
|
this.ctx.response = {
|
|
344
|
-
headers: omit(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
344
|
+
headers: omit$1(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
345
345
|
statusCode: res.statusCode,
|
|
346
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
346
|
+
body: trim$1(body, MAX_BODY_LENGTH$1)
|
|
347
347
|
};
|
|
348
348
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
349
349
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -357,7 +357,7 @@ class HTTPLogger extends BaseLogger {
|
|
|
357
357
|
let url = this.ctx.request.url;
|
|
358
358
|
let statusCode = this.ctx.response.statusCode;
|
|
359
359
|
let responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
360
|
-
let responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
360
|
+
let responseSize = formatBytes$1((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
361
361
|
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
362
362
|
}
|
|
363
363
|
success(req, res, body) {
|
|
@@ -450,8 +450,128 @@ function plugin(options = {}) {
|
|
|
450
450
|
});
|
|
451
451
|
};
|
|
452
452
|
}
|
|
453
|
+
const { redact, trim } = http;
|
|
454
|
+
const { formatBytes } = format;
|
|
455
|
+
const { omit } = object;
|
|
456
|
+
const MAX_BODY_LENGTH = 128;
|
|
457
|
+
class FastifyLogger extends BaseLogger {
|
|
458
|
+
constructor(startTime, options = {}) {
|
|
459
|
+
super(options["component"] || "fastify-api", options);
|
|
460
|
+
this.startTime = startTime || Date.now();
|
|
461
|
+
}
|
|
462
|
+
getRequestSessionId(req) {
|
|
463
|
+
return req.id;
|
|
464
|
+
}
|
|
465
|
+
getResponseHeaders(res) {
|
|
466
|
+
return res.headers;
|
|
467
|
+
}
|
|
468
|
+
getRequestSocketRemoteAddress(req) {
|
|
469
|
+
var _a;
|
|
470
|
+
return (_a = req.socket) == null ? void 0 : _a.remoteAddress;
|
|
471
|
+
}
|
|
472
|
+
getRequestUserId(req) {
|
|
473
|
+
var _a;
|
|
474
|
+
return (_a = req["user"]) == null ? void 0 : _a.id;
|
|
475
|
+
}
|
|
476
|
+
getResponseStatusCode(res) {
|
|
477
|
+
return res.statusCode;
|
|
478
|
+
}
|
|
479
|
+
getRequestQuery(req) {
|
|
480
|
+
return { ...req.query };
|
|
481
|
+
}
|
|
482
|
+
getRequestParams(req) {
|
|
483
|
+
return req.params;
|
|
484
|
+
}
|
|
485
|
+
getRequestBody(req) {
|
|
486
|
+
return req.body;
|
|
487
|
+
}
|
|
488
|
+
getRequestMethod(req) {
|
|
489
|
+
return req.method;
|
|
490
|
+
}
|
|
491
|
+
getRequestUrl(req) {
|
|
492
|
+
return req.url;
|
|
493
|
+
}
|
|
494
|
+
getRequestOriginalUrl(req) {
|
|
495
|
+
return req.originalUrl;
|
|
496
|
+
}
|
|
497
|
+
getRequestBaseUrl(req) {
|
|
498
|
+
const url = new URL(req.url, "http://" + req.headers.host);
|
|
499
|
+
return url.origin;
|
|
500
|
+
}
|
|
501
|
+
getRequestHeaders(req) {
|
|
502
|
+
return req.headers;
|
|
503
|
+
}
|
|
504
|
+
request(req) {
|
|
505
|
+
this.session(this.getRequestSessionId(req));
|
|
506
|
+
this.req = req;
|
|
507
|
+
return this;
|
|
508
|
+
}
|
|
509
|
+
response(res) {
|
|
510
|
+
this.res = res;
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
body(data) {
|
|
514
|
+
this.data = data;
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
_prepare() {
|
|
518
|
+
var _a;
|
|
519
|
+
const req = this.req;
|
|
520
|
+
const res = this.res;
|
|
521
|
+
const body = this.data;
|
|
522
|
+
this.ctx.request = {
|
|
523
|
+
headers: redact(this.getRequestHeaders(req), "cookie"),
|
|
524
|
+
host: this.getRequestHeaders(req).host,
|
|
525
|
+
baseUrl: this.getRequestBaseUrl(req),
|
|
526
|
+
url: this.getRequestOriginalUrl(req) || this.getRequestUrl(req),
|
|
527
|
+
method: this.getRequestMethod(req),
|
|
528
|
+
body: redact(this.getRequestBody(req), "password"),
|
|
529
|
+
params: this.getRequestParams(req),
|
|
530
|
+
query: redact(this.getRequestQuery(req), "password"),
|
|
531
|
+
clientIP: ((_a = this.getRequestHeaders(req)["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? this.getRequestSocketRemoteAddress(req)
|
|
532
|
+
};
|
|
533
|
+
this.ctx.response = {
|
|
534
|
+
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
535
|
+
statusCode: this.getResponseStatusCode(res),
|
|
536
|
+
body: trim(body, MAX_BODY_LENGTH)
|
|
537
|
+
};
|
|
538
|
+
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
539
|
+
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
540
|
+
this.ctx.user = this.getRequestUserId(req);
|
|
541
|
+
}
|
|
542
|
+
_message(msg) {
|
|
543
|
+
var _a;
|
|
544
|
+
const remoteAddress = this.getRemoteAddress(this.req);
|
|
545
|
+
const ip = this.ctx.request.clientIP;
|
|
546
|
+
const method = this.ctx.request.method;
|
|
547
|
+
const url = this.ctx.request.url;
|
|
548
|
+
const statusCode = this.ctx.response.statusCode;
|
|
549
|
+
const responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
550
|
+
const responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
551
|
+
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
552
|
+
}
|
|
553
|
+
getRemoteAddress(req) {
|
|
554
|
+
var _a;
|
|
555
|
+
return ((_a = req == null ? void 0 : req.headers["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? (req == null ? void 0 : req.socket.remoteAddress);
|
|
556
|
+
}
|
|
557
|
+
success(req, res, body) {
|
|
558
|
+
if (req)
|
|
559
|
+
this.request(req);
|
|
560
|
+
if (res)
|
|
561
|
+
this.response(res);
|
|
562
|
+
if (body)
|
|
563
|
+
this.body(body);
|
|
564
|
+
this._prepare();
|
|
565
|
+
super.info(this._message());
|
|
566
|
+
}
|
|
567
|
+
error(err) {
|
|
568
|
+
this._prepare();
|
|
569
|
+
super.error(this._message(err));
|
|
570
|
+
}
|
|
571
|
+
}
|
|
453
572
|
export {
|
|
454
573
|
BaseLogger,
|
|
574
|
+
FastifyLogger,
|
|
455
575
|
HTTPLogger as HttpLogger,
|
|
456
576
|
plugin as MongooseLoggerPlugin
|
|
457
577
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -142,16 +142,16 @@
|
|
|
142
142
|
levels: this.options.levels,
|
|
143
143
|
exitOnError: false,
|
|
144
144
|
// format,
|
|
145
|
-
transports: trans
|
|
146
|
-
exceptionHandlers
|
|
145
|
+
transports: trans
|
|
146
|
+
// exceptionHandlers
|
|
147
147
|
});
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
|
-
const redact$
|
|
150
|
+
const redact$2 = (data, ...sensitiveKeysList) => {
|
|
151
151
|
var _a, _b;
|
|
152
152
|
if (typeof data === "object" && data !== null && !((_b = (_a = data == null ? void 0 : data.constructor) == null ? void 0 : _a.name) == null ? void 0 : _b.startsWith("model"))) {
|
|
153
153
|
if (Array.isArray(data)) {
|
|
154
|
-
return data.map((item) => redact$
|
|
154
|
+
return data.map((item) => redact$2(item, ...sensitiveKeysList));
|
|
155
155
|
}
|
|
156
156
|
const redactedData = {};
|
|
157
157
|
for (const key in data) {
|
|
@@ -159,7 +159,7 @@
|
|
|
159
159
|
if (sensitiveKeysList.includes(key)) {
|
|
160
160
|
redactedData[key] = "*****";
|
|
161
161
|
} else {
|
|
162
|
-
redactedData[key] = redact$
|
|
162
|
+
redactedData[key] = redact$2(data[key], ...sensitiveKeysList);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
}
|
|
@@ -168,7 +168,7 @@
|
|
|
168
168
|
return data;
|
|
169
169
|
}
|
|
170
170
|
};
|
|
171
|
-
const trim$
|
|
171
|
+
const trim$2 = (data, length) => {
|
|
172
172
|
try {
|
|
173
173
|
let str = JSON.stringify(data);
|
|
174
174
|
if (str.length > length) {
|
|
@@ -181,8 +181,8 @@
|
|
|
181
181
|
};
|
|
182
182
|
const http = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
183
183
|
__proto__: null,
|
|
184
|
-
redact: redact$
|
|
185
|
-
trim: trim$
|
|
184
|
+
redact: redact$2,
|
|
185
|
+
trim: trim$2
|
|
186
186
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
187
187
|
function toFixedNumber(num, digits, base) {
|
|
188
188
|
const pow = Math.pow(base ?? 10, digits);
|
|
@@ -191,7 +191,7 @@
|
|
|
191
191
|
function bytesToMB$1(b) {
|
|
192
192
|
return toFixedNumber(b / 1024 / 1024, 2, 10);
|
|
193
193
|
}
|
|
194
|
-
function formatBytes$
|
|
194
|
+
function formatBytes$2(bytes, decimals = 2) {
|
|
195
195
|
if (!+bytes)
|
|
196
196
|
return "0 Bytes";
|
|
197
197
|
const k = 1024;
|
|
@@ -203,13 +203,13 @@
|
|
|
203
203
|
const format = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
204
204
|
__proto__: null,
|
|
205
205
|
bytesToMB: bytesToMB$1,
|
|
206
|
-
formatBytes: formatBytes$
|
|
206
|
+
formatBytes: formatBytes$2,
|
|
207
207
|
toFixedNumber
|
|
208
208
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
209
209
|
const pick = (obj, ...keys) => Object.fromEntries(
|
|
210
210
|
keys.filter((key) => key in obj).map((key) => [key, obj[key]])
|
|
211
211
|
);
|
|
212
|
-
const omit$
|
|
212
|
+
const omit$2 = (obj, ...keys) => {
|
|
213
213
|
const result = { ...obj };
|
|
214
214
|
keys.forEach(function(prop) {
|
|
215
215
|
delete result[prop];
|
|
@@ -218,7 +218,7 @@
|
|
|
218
218
|
};
|
|
219
219
|
const object = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
220
220
|
__proto__: null,
|
|
221
|
-
omit: omit$
|
|
221
|
+
omit: omit$2,
|
|
222
222
|
pick
|
|
223
223
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
224
224
|
const v8 = require("v8");
|
|
@@ -306,10 +306,10 @@
|
|
|
306
306
|
return this;
|
|
307
307
|
}
|
|
308
308
|
}
|
|
309
|
-
const { redact, trim } = http;
|
|
310
|
-
const { formatBytes } = format;
|
|
311
|
-
const { omit } = object;
|
|
312
|
-
const MAX_BODY_LENGTH = 128;
|
|
309
|
+
const { redact: redact$1, trim: trim$1 } = http;
|
|
310
|
+
const { formatBytes: formatBytes$1 } = format;
|
|
311
|
+
const { omit: omit$1 } = object;
|
|
312
|
+
const MAX_BODY_LENGTH$1 = 128;
|
|
313
313
|
class HTTPLogger extends BaseLogger {
|
|
314
314
|
constructor(startTime, options = {}) {
|
|
315
315
|
super("http", options);
|
|
@@ -334,20 +334,20 @@
|
|
|
334
334
|
let res = this.res;
|
|
335
335
|
let body = this.data;
|
|
336
336
|
this.ctx.request = {
|
|
337
|
-
headers: redact(req.headers, "cookie"),
|
|
337
|
+
headers: redact$1(req.headers, "cookie"),
|
|
338
338
|
host: req.headers.host,
|
|
339
339
|
baseUrl: req.baseUrl,
|
|
340
340
|
url: req.originalUrl || req.url,
|
|
341
341
|
method: req.method,
|
|
342
|
-
body: redact(req.body, "password"),
|
|
342
|
+
body: redact$1(req.body, "password"),
|
|
343
343
|
params: req == null ? void 0 : req.params,
|
|
344
|
-
query: redact(req == null ? void 0 : req.query, "password"),
|
|
344
|
+
query: redact$1(req == null ? void 0 : req.query, "password"),
|
|
345
345
|
clientIP: ((_a = req == null ? void 0 : req.headers["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? (req == null ? void 0 : req.socket.remoteAddress)
|
|
346
346
|
};
|
|
347
347
|
this.ctx.response = {
|
|
348
|
-
headers: omit(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
348
|
+
headers: omit$1(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
349
349
|
statusCode: res.statusCode,
|
|
350
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
350
|
+
body: trim$1(body, MAX_BODY_LENGTH$1)
|
|
351
351
|
};
|
|
352
352
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
353
353
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -361,7 +361,7 @@
|
|
|
361
361
|
let url = this.ctx.request.url;
|
|
362
362
|
let statusCode = this.ctx.response.statusCode;
|
|
363
363
|
let responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
364
|
-
let responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
364
|
+
let responseSize = formatBytes$1((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
365
365
|
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
366
366
|
}
|
|
367
367
|
success(req, res, body) {
|
|
@@ -454,7 +454,127 @@
|
|
|
454
454
|
});
|
|
455
455
|
};
|
|
456
456
|
}
|
|
457
|
+
const { redact, trim } = http;
|
|
458
|
+
const { formatBytes } = format;
|
|
459
|
+
const { omit } = object;
|
|
460
|
+
const MAX_BODY_LENGTH = 128;
|
|
461
|
+
class FastifyLogger extends BaseLogger {
|
|
462
|
+
constructor(startTime, options = {}) {
|
|
463
|
+
super(options["component"] || "fastify-api", options);
|
|
464
|
+
this.startTime = startTime || Date.now();
|
|
465
|
+
}
|
|
466
|
+
getRequestSessionId(req) {
|
|
467
|
+
return req.id;
|
|
468
|
+
}
|
|
469
|
+
getResponseHeaders(res) {
|
|
470
|
+
return res.headers;
|
|
471
|
+
}
|
|
472
|
+
getRequestSocketRemoteAddress(req) {
|
|
473
|
+
var _a;
|
|
474
|
+
return (_a = req.socket) == null ? void 0 : _a.remoteAddress;
|
|
475
|
+
}
|
|
476
|
+
getRequestUserId(req) {
|
|
477
|
+
var _a;
|
|
478
|
+
return (_a = req["user"]) == null ? void 0 : _a.id;
|
|
479
|
+
}
|
|
480
|
+
getResponseStatusCode(res) {
|
|
481
|
+
return res.statusCode;
|
|
482
|
+
}
|
|
483
|
+
getRequestQuery(req) {
|
|
484
|
+
return { ...req.query };
|
|
485
|
+
}
|
|
486
|
+
getRequestParams(req) {
|
|
487
|
+
return req.params;
|
|
488
|
+
}
|
|
489
|
+
getRequestBody(req) {
|
|
490
|
+
return req.body;
|
|
491
|
+
}
|
|
492
|
+
getRequestMethod(req) {
|
|
493
|
+
return req.method;
|
|
494
|
+
}
|
|
495
|
+
getRequestUrl(req) {
|
|
496
|
+
return req.url;
|
|
497
|
+
}
|
|
498
|
+
getRequestOriginalUrl(req) {
|
|
499
|
+
return req.originalUrl;
|
|
500
|
+
}
|
|
501
|
+
getRequestBaseUrl(req) {
|
|
502
|
+
const url = new URL(req.url, "http://" + req.headers.host);
|
|
503
|
+
return url.origin;
|
|
504
|
+
}
|
|
505
|
+
getRequestHeaders(req) {
|
|
506
|
+
return req.headers;
|
|
507
|
+
}
|
|
508
|
+
request(req) {
|
|
509
|
+
this.session(this.getRequestSessionId(req));
|
|
510
|
+
this.req = req;
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
response(res) {
|
|
514
|
+
this.res = res;
|
|
515
|
+
return this;
|
|
516
|
+
}
|
|
517
|
+
body(data) {
|
|
518
|
+
this.data = data;
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
_prepare() {
|
|
522
|
+
var _a;
|
|
523
|
+
const req = this.req;
|
|
524
|
+
const res = this.res;
|
|
525
|
+
const body = this.data;
|
|
526
|
+
this.ctx.request = {
|
|
527
|
+
headers: redact(this.getRequestHeaders(req), "cookie"),
|
|
528
|
+
host: this.getRequestHeaders(req).host,
|
|
529
|
+
baseUrl: this.getRequestBaseUrl(req),
|
|
530
|
+
url: this.getRequestOriginalUrl(req) || this.getRequestUrl(req),
|
|
531
|
+
method: this.getRequestMethod(req),
|
|
532
|
+
body: redact(this.getRequestBody(req), "password"),
|
|
533
|
+
params: this.getRequestParams(req),
|
|
534
|
+
query: redact(this.getRequestQuery(req), "password"),
|
|
535
|
+
clientIP: ((_a = this.getRequestHeaders(req)["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? this.getRequestSocketRemoteAddress(req)
|
|
536
|
+
};
|
|
537
|
+
this.ctx.response = {
|
|
538
|
+
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
539
|
+
statusCode: this.getResponseStatusCode(res),
|
|
540
|
+
body: trim(body, MAX_BODY_LENGTH)
|
|
541
|
+
};
|
|
542
|
+
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
543
|
+
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
544
|
+
this.ctx.user = this.getRequestUserId(req);
|
|
545
|
+
}
|
|
546
|
+
_message(msg) {
|
|
547
|
+
var _a;
|
|
548
|
+
const remoteAddress = this.getRemoteAddress(this.req);
|
|
549
|
+
const ip = this.ctx.request.clientIP;
|
|
550
|
+
const method = this.ctx.request.method;
|
|
551
|
+
const url = this.ctx.request.url;
|
|
552
|
+
const statusCode = this.ctx.response.statusCode;
|
|
553
|
+
const responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
554
|
+
const responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
555
|
+
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
556
|
+
}
|
|
557
|
+
getRemoteAddress(req) {
|
|
558
|
+
var _a;
|
|
559
|
+
return ((_a = req == null ? void 0 : req.headers["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? (req == null ? void 0 : req.socket.remoteAddress);
|
|
560
|
+
}
|
|
561
|
+
success(req, res, body) {
|
|
562
|
+
if (req)
|
|
563
|
+
this.request(req);
|
|
564
|
+
if (res)
|
|
565
|
+
this.response(res);
|
|
566
|
+
if (body)
|
|
567
|
+
this.body(body);
|
|
568
|
+
this._prepare();
|
|
569
|
+
super.info(this._message());
|
|
570
|
+
}
|
|
571
|
+
error(err) {
|
|
572
|
+
this._prepare();
|
|
573
|
+
super.error(this._message(err));
|
|
574
|
+
}
|
|
575
|
+
}
|
|
457
576
|
exports2.BaseLogger = BaseLogger;
|
|
577
|
+
exports2.FastifyLogger = FastifyLogger;
|
|
458
578
|
exports2.HttpLogger = HTTPLogger;
|
|
459
579
|
exports2.MongooseLoggerPlugin = plugin;
|
|
460
580
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
package/dist/main.d.ts
CHANGED
|
@@ -20,13 +20,27 @@ export class BaseLogger {
|
|
|
20
20
|
export class HTTPLogger extends BaseLogger {
|
|
21
21
|
constructor(startTime: number, options?: any);
|
|
22
22
|
|
|
23
|
-
request(req:
|
|
23
|
+
request(req: object): this;
|
|
24
24
|
|
|
25
|
-
response(res:
|
|
25
|
+
response(res: object): this;
|
|
26
26
|
|
|
27
27
|
body(data: any): this;
|
|
28
28
|
|
|
29
|
-
success(req:
|
|
29
|
+
success(req: object, res: object, body?: any): void;
|
|
30
|
+
|
|
31
|
+
error(err: any): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class FastifyLogger extends BaseLogger {
|
|
35
|
+
constructor(startTime: number, options?: any);
|
|
36
|
+
|
|
37
|
+
request(req: object): this;
|
|
38
|
+
|
|
39
|
+
response(res: object): this;
|
|
40
|
+
|
|
41
|
+
body(data: any): this;
|
|
42
|
+
|
|
43
|
+
success(req?: object, res?: object, body?: any): void;
|
|
30
44
|
|
|
31
45
|
error(err: any): void;
|
|
32
46
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "infront-logger",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
33
33
|
"dev": "vite",
|
|
34
34
|
"build": "vite build",
|
|
35
|
+
"build:watch": "vite build --watch",
|
|
35
36
|
"preview": "vite preview"
|
|
36
37
|
}
|
|
37
38
|
}
|