infront-logger 1.1.4 → 1.1.6
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/README.MD +9 -0
- package/dist/browser.d.ts +12 -0
- package/dist/browser.es.js +4176 -0
- package/dist/browser.umd.js +4180 -0
- package/package.json +14 -4
- package/dist/index.es.js +0 -598
- package/dist/index.umd.js +0 -602
package/dist/index.umd.js
DELETED
|
@@ -1,602 +0,0 @@
|
|
|
1
|
-
(function(global, factory) {
|
|
2
|
-
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.index = {}));
|
|
3
|
-
})(this, function(exports2) {
|
|
4
|
-
"use strict";
|
|
5
|
-
require("winston-daily-rotate-file");
|
|
6
|
-
const { format: format$1, createLogger, transports, addColors } = require("winston");
|
|
7
|
-
require("util");
|
|
8
|
-
const colors = {
|
|
9
|
-
error: "red",
|
|
10
|
-
warn: "yellow",
|
|
11
|
-
info: "green",
|
|
12
|
-
http: "magenta",
|
|
13
|
-
debug: "white"
|
|
14
|
-
};
|
|
15
|
-
addColors(colors);
|
|
16
|
-
const OPTIONS$1 = {
|
|
17
|
-
dirname: "logs",
|
|
18
|
-
levels: {
|
|
19
|
-
error: 0,
|
|
20
|
-
warn: 1,
|
|
21
|
-
info: 2,
|
|
22
|
-
http: 3,
|
|
23
|
-
debug: 4
|
|
24
|
-
},
|
|
25
|
-
level: "info",
|
|
26
|
-
zippedArchive: false,
|
|
27
|
-
dateFormat: "YYYY-MM-DD HH:mm:ss:ms",
|
|
28
|
-
maxFiles: 2,
|
|
29
|
-
maxFileSize: "30m",
|
|
30
|
-
filename: "logs.log",
|
|
31
|
-
errorFilename: "error.log",
|
|
32
|
-
console: true,
|
|
33
|
-
file: true,
|
|
34
|
-
exclude: [],
|
|
35
|
-
createSymlink: true,
|
|
36
|
-
symlinkName: "logs.log",
|
|
37
|
-
consoleJSON: false
|
|
38
|
-
};
|
|
39
|
-
function fileFormatter(options) {
|
|
40
|
-
return format$1.combine(
|
|
41
|
-
format$1.errors({ stack: true }),
|
|
42
|
-
// {
|
|
43
|
-
// transform: (info) => {
|
|
44
|
-
// const args = [info.message, ...(info[Symbol.for('splat')] || [])];
|
|
45
|
-
// info.message = args.filter(Boolean).map(arg => {
|
|
46
|
-
// if(arg instanceof Error){
|
|
47
|
-
// return errorToJSON(arg);
|
|
48
|
-
// }
|
|
49
|
-
// return arg;
|
|
50
|
-
// });
|
|
51
|
-
//
|
|
52
|
-
// const msg = args.map(arg => {
|
|
53
|
-
// if (typeof arg == 'object')
|
|
54
|
-
// return inspect(arg, {compact: false, depth: Infinity});
|
|
55
|
-
// return arg;
|
|
56
|
-
// }).join(' ');
|
|
57
|
-
//
|
|
58
|
-
// info[Symbol.for('message')] = `${info[Symbol.for('level')]}: ${msg}${info.stack ? ' ' + info.stack : ''}`;
|
|
59
|
-
//
|
|
60
|
-
// if(options.exclude.some(string => msg.includes(string))) return null;
|
|
61
|
-
//
|
|
62
|
-
// return info;
|
|
63
|
-
// }
|
|
64
|
-
// },
|
|
65
|
-
format$1.timestamp({ format: options.dateFormat }),
|
|
66
|
-
format$1.json()
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
function consoleFormatter(options) {
|
|
70
|
-
let formatters = [
|
|
71
|
-
format$1.errors({ stack: true }),
|
|
72
|
-
format$1.printf((info) => {
|
|
73
|
-
if ((info == null ? void 0 : info.level) === "error" && info.stack) {
|
|
74
|
-
return `${info.stack}`;
|
|
75
|
-
} else {
|
|
76
|
-
return info.message;
|
|
77
|
-
}
|
|
78
|
-
}),
|
|
79
|
-
// options.consoleJSON? format.json() : {transform : (info)=>info},
|
|
80
|
-
options.consoleJSON ? format$1.json() : format$1.splat()
|
|
81
|
-
];
|
|
82
|
-
if (!options.consoleJSON) {
|
|
83
|
-
formatters.push(format$1.colorize({ all: true }));
|
|
84
|
-
}
|
|
85
|
-
return format$1.combine(...formatters);
|
|
86
|
-
}
|
|
87
|
-
function getFormatter(type, options) {
|
|
88
|
-
switch (type) {
|
|
89
|
-
case "file":
|
|
90
|
-
return fileFormatter(options);
|
|
91
|
-
case "access":
|
|
92
|
-
break;
|
|
93
|
-
case "console":
|
|
94
|
-
return consoleFormatter(options);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
function createTransport(options) {
|
|
98
|
-
let formatter = getFormatter(options.format, options);
|
|
99
|
-
if (formatter) {
|
|
100
|
-
options.format = formatter;
|
|
101
|
-
} else {
|
|
102
|
-
delete options.format;
|
|
103
|
-
}
|
|
104
|
-
return new transports.DailyRotateFile(options);
|
|
105
|
-
}
|
|
106
|
-
class Logger {
|
|
107
|
-
constructor(options = {}) {
|
|
108
|
-
this.options = { ...OPTIONS$1, ...options };
|
|
109
|
-
if (!this.options.filename.includes("."))
|
|
110
|
-
this.options.filename += ".log";
|
|
111
|
-
if (!this.options.errorFilename.includes("."))
|
|
112
|
-
this.options.errorFilename += ".log";
|
|
113
|
-
let trans = [];
|
|
114
|
-
let exceptionHandlers = [new transports.Console({
|
|
115
|
-
format: getFormatter("console", this.options)
|
|
116
|
-
})];
|
|
117
|
-
if (this.options.file) {
|
|
118
|
-
trans.push(createTransport({
|
|
119
|
-
...this.options,
|
|
120
|
-
format: "file",
|
|
121
|
-
filename: this.options.filename,
|
|
122
|
-
symlinkName: this.options.filename
|
|
123
|
-
}));
|
|
124
|
-
trans.push(createTransport({
|
|
125
|
-
...this.options,
|
|
126
|
-
format: "file",
|
|
127
|
-
filename: this.options.errorFilename,
|
|
128
|
-
symlinkName: this.options.errorFilename,
|
|
129
|
-
level: "error"
|
|
130
|
-
}));
|
|
131
|
-
exceptionHandlers.push(
|
|
132
|
-
new transports.File({ filename: `${this.options.dirname}/${this.options.errorFilename}` })
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
if (this.options.console) {
|
|
136
|
-
trans.push(new transports.Console({
|
|
137
|
-
format: getFormatter("console", this.options)
|
|
138
|
-
}));
|
|
139
|
-
}
|
|
140
|
-
this.logger = createLogger({
|
|
141
|
-
level: this.options.level,
|
|
142
|
-
levels: this.options.levels,
|
|
143
|
-
exitOnError: false,
|
|
144
|
-
// format,
|
|
145
|
-
transports: trans
|
|
146
|
-
// exceptionHandlers
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const redact$2 = (data, ...sensitiveKeysList) => {
|
|
151
|
-
var _a, _b;
|
|
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
|
-
if (Array.isArray(data)) {
|
|
154
|
-
return data.map((item) => redact$2(item, ...sensitiveKeysList));
|
|
155
|
-
}
|
|
156
|
-
const redactedData = {};
|
|
157
|
-
for (const key in data) {
|
|
158
|
-
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
159
|
-
if (sensitiveKeysList.includes(key)) {
|
|
160
|
-
redactedData[key] = "*****";
|
|
161
|
-
} else {
|
|
162
|
-
redactedData[key] = redact$2(data[key], ...sensitiveKeysList);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return redactedData;
|
|
167
|
-
} else {
|
|
168
|
-
return data;
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
const trim$2 = (data, length) => {
|
|
172
|
-
try {
|
|
173
|
-
let str = JSON.stringify(data);
|
|
174
|
-
if (str.length > length) {
|
|
175
|
-
return str.substring(0, length) + "... [TRIMMED]";
|
|
176
|
-
}
|
|
177
|
-
return data;
|
|
178
|
-
} catch (err) {
|
|
179
|
-
return "";
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
const http = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
183
|
-
__proto__: null,
|
|
184
|
-
redact: redact$2,
|
|
185
|
-
trim: trim$2
|
|
186
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
187
|
-
function toFixedNumber(num, digits, base) {
|
|
188
|
-
const pow = Math.pow(base ?? 10, digits);
|
|
189
|
-
return Math.round(num * pow) / pow;
|
|
190
|
-
}
|
|
191
|
-
function bytesToMB$1(b) {
|
|
192
|
-
return toFixedNumber(b / 1024 / 1024, 2, 10);
|
|
193
|
-
}
|
|
194
|
-
function formatBytes$2(bytes, decimals = 2) {
|
|
195
|
-
if (!+bytes)
|
|
196
|
-
return "0 Bytes";
|
|
197
|
-
const k = 1024;
|
|
198
|
-
const dm = decimals < 0 ? 0 : decimals;
|
|
199
|
-
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
200
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
201
|
-
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))}${sizes[i]}`;
|
|
202
|
-
}
|
|
203
|
-
const format = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
204
|
-
__proto__: null,
|
|
205
|
-
bytesToMB: bytesToMB$1,
|
|
206
|
-
formatBytes: formatBytes$2,
|
|
207
|
-
toFixedNumber
|
|
208
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
209
|
-
const pick = (obj, ...keys) => Object.fromEntries(
|
|
210
|
-
keys.filter((key) => key in obj).map((key) => [key, obj[key]])
|
|
211
|
-
);
|
|
212
|
-
const omit$2 = (obj, ...keys) => {
|
|
213
|
-
const result = { ...obj };
|
|
214
|
-
keys.forEach(function(prop) {
|
|
215
|
-
delete result[prop];
|
|
216
|
-
});
|
|
217
|
-
return result;
|
|
218
|
-
};
|
|
219
|
-
const object = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
220
|
-
__proto__: null,
|
|
221
|
-
omit: omit$2,
|
|
222
|
-
pick
|
|
223
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
224
|
-
const v8 = require("v8");
|
|
225
|
-
const { bytesToMB } = format;
|
|
226
|
-
function stats() {
|
|
227
|
-
try {
|
|
228
|
-
let h = v8.getHeapStatistics();
|
|
229
|
-
return {
|
|
230
|
-
"total_heap_size": bytesToMB(h.total_heap_size),
|
|
231
|
-
"total_heap_size_executable": bytesToMB(h.total_heap_size_executable),
|
|
232
|
-
"total_physical_size": bytesToMB(h.total_physical_size),
|
|
233
|
-
"total_available_size": bytesToMB(h.total_available_size),
|
|
234
|
-
"used_heap_size": bytesToMB(h.used_heap_size),
|
|
235
|
-
"heap_size_limit": bytesToMB(h.heap_size_limit),
|
|
236
|
-
"malloced_memory": bytesToMB(h.malloced_memory),
|
|
237
|
-
"peak_malloced_memory": bytesToMB(h.peak_malloced_memory),
|
|
238
|
-
"does_zap_garbage": h.does_zap_garbage,
|
|
239
|
-
"number_of_native_contexts": h.number_of_native_contexts,
|
|
240
|
-
"number_of_detached_contexts": h.number_of_detached_contexts
|
|
241
|
-
};
|
|
242
|
-
} catch (err) {
|
|
243
|
-
return {};
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
class BaseLogger {
|
|
247
|
-
constructor(component, options) {
|
|
248
|
-
this.options = options;
|
|
249
|
-
this.logger = new Logger(options).logger.child({ component });
|
|
250
|
-
this.ctx = {};
|
|
251
|
-
this.startTime = Date.now();
|
|
252
|
-
this.absaluteStartTime = Date.now();
|
|
253
|
-
}
|
|
254
|
-
session(id) {
|
|
255
|
-
this.ctx.sessionID = id;
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
log() {
|
|
259
|
-
this.logger.info(...arguments, this.ctx);
|
|
260
|
-
this._stopMemProfile();
|
|
261
|
-
}
|
|
262
|
-
info() {
|
|
263
|
-
this.logger.info(...arguments, this.ctx);
|
|
264
|
-
this._stopMemProfile();
|
|
265
|
-
}
|
|
266
|
-
error() {
|
|
267
|
-
this.logger.error(...arguments, this.ctx);
|
|
268
|
-
this._stopMemProfile();
|
|
269
|
-
}
|
|
270
|
-
profile(action, options = {}) {
|
|
271
|
-
this.ctx.profiler = this.ctx.profiler || {};
|
|
272
|
-
if (action) {
|
|
273
|
-
let startTime = this.ctx.profiler[action] ? Date.now() - this.ctx.profiler[action] : this.startTime;
|
|
274
|
-
this.ctx.profiler[action] = Date.now() - startTime;
|
|
275
|
-
}
|
|
276
|
-
this.ctx.profiler.totalTime = Date.now() - this.absaluteStartTime;
|
|
277
|
-
if (!options.continue)
|
|
278
|
-
this.startTime = Date.now();
|
|
279
|
-
return this;
|
|
280
|
-
}
|
|
281
|
-
_stopMemProfile() {
|
|
282
|
-
if (this.memProfileInterval)
|
|
283
|
-
clearInterval(this.memProfileInterval);
|
|
284
|
-
}
|
|
285
|
-
profileMem(options = {}) {
|
|
286
|
-
this.ctx.maxMemory = this.ctx.maxMemory || 0;
|
|
287
|
-
this.ctx.memoryStats = this.ctx.memoryStats || [];
|
|
288
|
-
this.ctx.memoryUsage = this.ctx.memoryUsage || [];
|
|
289
|
-
this.ctx.memoryStatsIntervalMS = options.interval || 1e3;
|
|
290
|
-
this._stopMemProfile();
|
|
291
|
-
this.memProfileInterval = setInterval(() => {
|
|
292
|
-
let mem = stats();
|
|
293
|
-
this.ctx.memoryStats.push(mem);
|
|
294
|
-
this.ctx.memoryUsage.push(mem.used_heap_size);
|
|
295
|
-
if (mem.used_heap_size > this.ctx.maxMemory) {
|
|
296
|
-
this.ctx.maxMemory = mem.used_heap_size;
|
|
297
|
-
}
|
|
298
|
-
}, this.ctx.memoryStatsIntervalMS);
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
301
|
-
context(key, value) {
|
|
302
|
-
if (typeof key === "string") {
|
|
303
|
-
this.ctx[key] = value;
|
|
304
|
-
} else if (typeof key === "object") {
|
|
305
|
-
Object.assign(this.ctx, key);
|
|
306
|
-
}
|
|
307
|
-
return this;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
const { redact: redact$1, trim: trim$1 } = http;
|
|
311
|
-
const { formatBytes: formatBytes$1 } = format;
|
|
312
|
-
const { omit: omit$1 } = object;
|
|
313
|
-
const MAX_BODY_LENGTH$1 = 128;
|
|
314
|
-
class HTTPLogger extends BaseLogger {
|
|
315
|
-
constructor(startTime, options = {}) {
|
|
316
|
-
super("http", options);
|
|
317
|
-
this.startTime = startTime || Date.now();
|
|
318
|
-
}
|
|
319
|
-
request(req) {
|
|
320
|
-
this.session(req.sessionID);
|
|
321
|
-
this.req = req;
|
|
322
|
-
return this;
|
|
323
|
-
}
|
|
324
|
-
response(res) {
|
|
325
|
-
this.res = res;
|
|
326
|
-
return this;
|
|
327
|
-
}
|
|
328
|
-
body(data) {
|
|
329
|
-
this.data = data;
|
|
330
|
-
return this;
|
|
331
|
-
}
|
|
332
|
-
_prepare() {
|
|
333
|
-
var _a, _b;
|
|
334
|
-
let req = this.req;
|
|
335
|
-
let res = this.res;
|
|
336
|
-
let body = this.data;
|
|
337
|
-
this.ctx.request = {
|
|
338
|
-
headers: redact$1(req.headers, "cookie"),
|
|
339
|
-
host: req.headers.host,
|
|
340
|
-
baseUrl: req.baseUrl,
|
|
341
|
-
url: req.originalUrl || req.url,
|
|
342
|
-
method: req.method,
|
|
343
|
-
body: redact$1(req.body, "password"),
|
|
344
|
-
params: req == null ? void 0 : req.params,
|
|
345
|
-
query: redact$1(req == null ? void 0 : req.query, "password"),
|
|
346
|
-
clientIP: ((_a = req == null ? void 0 : req.headers["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? (req == null ? void 0 : req.socket.remoteAddress)
|
|
347
|
-
};
|
|
348
|
-
this.ctx.response = {
|
|
349
|
-
headers: omit$1(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
350
|
-
statusCode: res.statusCode,
|
|
351
|
-
body: trim$1(body, MAX_BODY_LENGTH$1)
|
|
352
|
-
};
|
|
353
|
-
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
354
|
-
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
355
|
-
this.ctx.user = (_b = req == null ? void 0 : req.user) == null ? void 0 : _b.id;
|
|
356
|
-
}
|
|
357
|
-
_message(msg) {
|
|
358
|
-
var _a;
|
|
359
|
-
let remoteAddress = this.req.ip || this.req._remoteAddress || this.req.connection && this.req.connection.remoteAddress || void 0;
|
|
360
|
-
let ip = this.ctx.request.clientIP;
|
|
361
|
-
let method = this.ctx.request.method;
|
|
362
|
-
let url = this.ctx.request.url;
|
|
363
|
-
let statusCode = this.ctx.response.statusCode;
|
|
364
|
-
let responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
365
|
-
let responseSize = formatBytes$1((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
366
|
-
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
367
|
-
}
|
|
368
|
-
success(req, res, body) {
|
|
369
|
-
if (req)
|
|
370
|
-
this.request(req);
|
|
371
|
-
if (res)
|
|
372
|
-
this.response(res);
|
|
373
|
-
if (body)
|
|
374
|
-
this.body(body);
|
|
375
|
-
this._prepare();
|
|
376
|
-
super.info(this._message());
|
|
377
|
-
}
|
|
378
|
-
error(err) {
|
|
379
|
-
this._prepare();
|
|
380
|
-
super.error(this._message(err));
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
const ops = [
|
|
384
|
-
"find",
|
|
385
|
-
"findOne",
|
|
386
|
-
"findById",
|
|
387
|
-
"findOneAndUpdate",
|
|
388
|
-
"updateOne",
|
|
389
|
-
"updateMany",
|
|
390
|
-
"countDocuments",
|
|
391
|
-
"estimatedDocumentCount",
|
|
392
|
-
"findOneAndRemove",
|
|
393
|
-
"findOneAndDelete",
|
|
394
|
-
"deleteOne",
|
|
395
|
-
"deleteMany",
|
|
396
|
-
"aggregate",
|
|
397
|
-
"save"
|
|
398
|
-
];
|
|
399
|
-
const explained = [
|
|
400
|
-
"find",
|
|
401
|
-
"findOne"
|
|
402
|
-
];
|
|
403
|
-
const OPTIONS = {
|
|
404
|
-
maxResBytes: 100 * 1024,
|
|
405
|
-
logRes: true,
|
|
406
|
-
explain: false
|
|
407
|
-
};
|
|
408
|
-
class MongooseLogger extends BaseLogger {
|
|
409
|
-
constructor(options = {}) {
|
|
410
|
-
super("database", { ...OPTIONS, ...options });
|
|
411
|
-
}
|
|
412
|
-
operation(o) {
|
|
413
|
-
this.ctx.operation = o;
|
|
414
|
-
return this;
|
|
415
|
-
}
|
|
416
|
-
collection(c) {
|
|
417
|
-
this.ctx.collection = c;
|
|
418
|
-
return this;
|
|
419
|
-
}
|
|
420
|
-
query(q) {
|
|
421
|
-
this.ctx.query = q;
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
update(u) {
|
|
425
|
-
this.ctx.update = u;
|
|
426
|
-
return this;
|
|
427
|
-
}
|
|
428
|
-
pipeline(p) {
|
|
429
|
-
this.ctx.pipeline = p;
|
|
430
|
-
return this;
|
|
431
|
-
}
|
|
432
|
-
async explain(query) {
|
|
433
|
-
if (this.options.explain && !query.options.explain) {
|
|
434
|
-
this.ctx.explain = await query.clone().explain();
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
result(res) {
|
|
438
|
-
try {
|
|
439
|
-
this.ctx.resultSizeBytes = JSON.stringify(res).length;
|
|
440
|
-
this.ctx.resultLength = Array.isArray(res) ? res.length : res ? 1 : 0;
|
|
441
|
-
} catch (err) {
|
|
442
|
-
}
|
|
443
|
-
if (!this.options.logRes)
|
|
444
|
-
return this;
|
|
445
|
-
if (Array.isArray(res)) {
|
|
446
|
-
this.ctx.documentCount = res.length;
|
|
447
|
-
} else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
|
|
448
|
-
this.ctx.result = res;
|
|
449
|
-
} else {
|
|
450
|
-
this.ctx.result = `Result too long (more then ${this.options.maxResBytes} bytes)`;
|
|
451
|
-
}
|
|
452
|
-
return this;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
async function postHook(target, res) {
|
|
456
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
457
|
-
if ((_a = target == null ? void 0 : target.options) == null ? void 0 : _a.explain)
|
|
458
|
-
return;
|
|
459
|
-
const op = target.constructor.name === "Aggregate" ? "aggregate" : target.op || target.$op;
|
|
460
|
-
const collection = ((_b = target == null ? void 0 : target._collection) == null ? void 0 : _b.collectionName) || ((_c = target == null ? void 0 : target.collection) == null ? void 0 : _c.name) || ((_e = (_d = target == null ? void 0 : target._model) == null ? void 0 : _d.collection) == null ? void 0 : _e.collectionName);
|
|
461
|
-
(_f = target == null ? void 0 : target.logger) == null ? void 0 : _f.operation(op).collection(collection).query(target._conditions).update(target._update).pipeline(target._pipeline).profile().result(res);
|
|
462
|
-
if (explained.includes(op)) {
|
|
463
|
-
await (target == null ? void 0 : target.logger.explain(target));
|
|
464
|
-
}
|
|
465
|
-
(_g = target == null ? void 0 : target.logger) == null ? void 0 : _g.info(`DB query: ${op} - ${collection}`);
|
|
466
|
-
}
|
|
467
|
-
function plugin(options = {}) {
|
|
468
|
-
return (schema) => {
|
|
469
|
-
ops.forEach((op) => {
|
|
470
|
-
schema.pre(op, function(next) {
|
|
471
|
-
this.logger = new MongooseLogger(options);
|
|
472
|
-
next();
|
|
473
|
-
});
|
|
474
|
-
schema.post(op, function(res) {
|
|
475
|
-
postHook(this, res);
|
|
476
|
-
});
|
|
477
|
-
});
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
const { redact, trim } = http;
|
|
481
|
-
const { formatBytes } = format;
|
|
482
|
-
const { omit } = object;
|
|
483
|
-
const MAX_BODY_LENGTH = 128;
|
|
484
|
-
class FastifyLogger extends BaseLogger {
|
|
485
|
-
constructor(startTime, options = {}) {
|
|
486
|
-
super(options["component"] || "fastify-api", options);
|
|
487
|
-
this.startTime = startTime || Date.now();
|
|
488
|
-
}
|
|
489
|
-
getRequestSessionId(req) {
|
|
490
|
-
return req.id;
|
|
491
|
-
}
|
|
492
|
-
getResponseHeaders(res) {
|
|
493
|
-
return res.headers;
|
|
494
|
-
}
|
|
495
|
-
getRequestUserId(req) {
|
|
496
|
-
var _a;
|
|
497
|
-
return (_a = req["user"]) == null ? void 0 : _a.id;
|
|
498
|
-
}
|
|
499
|
-
getResponseStatusCode(res) {
|
|
500
|
-
return res.statusCode;
|
|
501
|
-
}
|
|
502
|
-
getRequestQuery(req) {
|
|
503
|
-
return req.query;
|
|
504
|
-
}
|
|
505
|
-
getRequestParams(req) {
|
|
506
|
-
return req.params;
|
|
507
|
-
}
|
|
508
|
-
getRequestBody(req) {
|
|
509
|
-
return req.body;
|
|
510
|
-
}
|
|
511
|
-
getRequestMethod(req) {
|
|
512
|
-
return req.method;
|
|
513
|
-
}
|
|
514
|
-
getRequestUrl(req) {
|
|
515
|
-
return req.url;
|
|
516
|
-
}
|
|
517
|
-
getRequestOriginalUrl(req) {
|
|
518
|
-
return req.originalUrl;
|
|
519
|
-
}
|
|
520
|
-
getRequestBaseUrl(req) {
|
|
521
|
-
const url = new URL(req.url, "http://" + req.headers.host);
|
|
522
|
-
return url.origin;
|
|
523
|
-
}
|
|
524
|
-
getRequestHeaders(req) {
|
|
525
|
-
return req.headers || {};
|
|
526
|
-
}
|
|
527
|
-
request(req) {
|
|
528
|
-
this.session(this.getRequestSessionId(req));
|
|
529
|
-
this.req = req;
|
|
530
|
-
return this;
|
|
531
|
-
}
|
|
532
|
-
response(res) {
|
|
533
|
-
this.res = res;
|
|
534
|
-
return this;
|
|
535
|
-
}
|
|
536
|
-
body(data) {
|
|
537
|
-
this.data = data;
|
|
538
|
-
return this;
|
|
539
|
-
}
|
|
540
|
-
_prepare() {
|
|
541
|
-
const req = this.req;
|
|
542
|
-
const res = this.res;
|
|
543
|
-
const body = this.data;
|
|
544
|
-
this.ctx.request = {
|
|
545
|
-
headers: redact(this.getRequestHeaders(req), "cookie"),
|
|
546
|
-
host: this.getRequestHeaders(req).host,
|
|
547
|
-
baseUrl: this.getRequestBaseUrl(req),
|
|
548
|
-
url: this.getRequestOriginalUrl(req) || this.getRequestUrl(req),
|
|
549
|
-
method: this.getRequestMethod(req),
|
|
550
|
-
body: redact(this.getRequestBody(req), "password"),
|
|
551
|
-
params: this.getRequestParams(req),
|
|
552
|
-
query: redact(this.getRequestQuery(req), "password"),
|
|
553
|
-
clientIP: this.getClientIp(req)
|
|
554
|
-
};
|
|
555
|
-
this.ctx.response = {
|
|
556
|
-
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
557
|
-
statusCode: this.getResponseStatusCode(res),
|
|
558
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
559
|
-
};
|
|
560
|
-
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
561
|
-
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
562
|
-
this.ctx.user = this.getRequestUserId(req);
|
|
563
|
-
}
|
|
564
|
-
_message(msg) {
|
|
565
|
-
var _a;
|
|
566
|
-
const remoteAddress = this.getRemoteAddress();
|
|
567
|
-
const ip = this.ctx.request.clientIP;
|
|
568
|
-
const method = this.ctx.request.method;
|
|
569
|
-
const url = this.ctx.request.url;
|
|
570
|
-
const statusCode = this.ctx.response.statusCode;
|
|
571
|
-
const responseTimeMs = this.ctx.responseTimeMs + "ms";
|
|
572
|
-
const responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
|
|
573
|
-
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
574
|
-
}
|
|
575
|
-
getClientIp(req) {
|
|
576
|
-
var _a, _b;
|
|
577
|
-
return ((_a = this.getRequestHeaders(req)["x-forwarded-for"]) == null ? void 0 : _a.split(",")[0]) ?? ((_b = req == null ? void 0 : req.socket) == null ? void 0 : _b.remoteAddress);
|
|
578
|
-
}
|
|
579
|
-
getRemoteAddress() {
|
|
580
|
-
return this.req.ip || this.req._remoteAddress || void 0;
|
|
581
|
-
}
|
|
582
|
-
success(req, res, body) {
|
|
583
|
-
if (req)
|
|
584
|
-
this.request(req);
|
|
585
|
-
if (res)
|
|
586
|
-
this.response(res);
|
|
587
|
-
if (body)
|
|
588
|
-
this.body(body);
|
|
589
|
-
this._prepare();
|
|
590
|
-
super.info(this._message());
|
|
591
|
-
}
|
|
592
|
-
error(err) {
|
|
593
|
-
this._prepare();
|
|
594
|
-
super.error(this._message(err));
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
exports2.BaseLogger = BaseLogger;
|
|
598
|
-
exports2.FastifyLogger = FastifyLogger;
|
|
599
|
-
exports2.HttpLogger = HTTPLogger;
|
|
600
|
-
exports2.MongooseLoggerPlugin = plugin;
|
|
601
|
-
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
602
|
-
});
|