infront-logger 1.1.1 → 1.1.3
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 +32 -9
- package/dist/index.umd.js +32 -9
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -9,7 +9,7 @@ const colors = {
|
|
|
9
9
|
debug: "white"
|
|
10
10
|
};
|
|
11
11
|
addColors(colors);
|
|
12
|
-
const OPTIONS = {
|
|
12
|
+
const OPTIONS$1 = {
|
|
13
13
|
dirname: "logs",
|
|
14
14
|
levels: {
|
|
15
15
|
error: 0,
|
|
@@ -101,7 +101,7 @@ function createTransport(options) {
|
|
|
101
101
|
}
|
|
102
102
|
class Logger {
|
|
103
103
|
constructor(options = {}) {
|
|
104
|
-
this.options = { ...OPTIONS, ...options };
|
|
104
|
+
this.options = { ...OPTIONS$1, ...options };
|
|
105
105
|
if (!this.options.filename.includes("."))
|
|
106
106
|
this.options.filename += ".log";
|
|
107
107
|
if (!this.options.errorFilename.includes("."))
|
|
@@ -241,6 +241,7 @@ function stats() {
|
|
|
241
241
|
}
|
|
242
242
|
class BaseLogger {
|
|
243
243
|
constructor(component, options) {
|
|
244
|
+
this.options = options;
|
|
244
245
|
this.logger = new Logger(options).logger.child({ component });
|
|
245
246
|
this.ctx = {};
|
|
246
247
|
this.startTime = Date.now();
|
|
@@ -391,10 +392,18 @@ const ops = [
|
|
|
391
392
|
"aggregate",
|
|
392
393
|
"save"
|
|
393
394
|
];
|
|
394
|
-
const
|
|
395
|
+
const explained = [
|
|
396
|
+
"find",
|
|
397
|
+
"findOne"
|
|
398
|
+
];
|
|
399
|
+
const OPTIONS = {
|
|
400
|
+
maxResBytes: 100 * 1024,
|
|
401
|
+
logRes: true,
|
|
402
|
+
explain: false
|
|
403
|
+
};
|
|
395
404
|
class MongooseLogger extends BaseLogger {
|
|
396
405
|
constructor(options = {}) {
|
|
397
|
-
super("database", options);
|
|
406
|
+
super("database", { ...OPTIONS, ...options });
|
|
398
407
|
}
|
|
399
408
|
operation(o) {
|
|
400
409
|
this.ctx.operation = o;
|
|
@@ -416,26 +425,40 @@ class MongooseLogger extends BaseLogger {
|
|
|
416
425
|
this.ctx.pipeline = p;
|
|
417
426
|
return this;
|
|
418
427
|
}
|
|
428
|
+
async explain(query) {
|
|
429
|
+
if (this.options.explain && !query.options.explain) {
|
|
430
|
+
this.ctx.explain = await query.clone().explain();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
419
433
|
result(res) {
|
|
420
434
|
try {
|
|
421
435
|
this.ctx.resultSizeBytes = JSON.stringify(res).length;
|
|
436
|
+
this.ctx.resultLength = Array.isArray(res) ? res.length : res ? 1 : 0;
|
|
422
437
|
} catch (err) {
|
|
423
438
|
}
|
|
439
|
+
if (!this.options.logRes)
|
|
440
|
+
return this;
|
|
424
441
|
if (Array.isArray(res)) {
|
|
425
442
|
this.ctx.documentCount = res.length;
|
|
426
|
-
} else if (this.ctx.resultSizeBytes <
|
|
443
|
+
} else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
|
|
427
444
|
this.ctx.result = res;
|
|
428
445
|
} else {
|
|
429
|
-
this.ctx.result = `Result too long (more then ${
|
|
446
|
+
this.ctx.result = `Result too long (more then ${this.options.maxResBytes} bytes)`;
|
|
430
447
|
}
|
|
431
448
|
return this;
|
|
432
449
|
}
|
|
433
450
|
}
|
|
434
|
-
function postHook(target, res) {
|
|
435
|
-
var _a, _b, _c, _d, _e;
|
|
451
|
+
async function postHook(target, res) {
|
|
452
|
+
var _a, _b, _c, _d, _e, _f;
|
|
453
|
+
if (target.options.explain)
|
|
454
|
+
return;
|
|
436
455
|
const op = target.constructor.name === "Aggregate" ? "aggregate" : target.op || target.$op;
|
|
437
456
|
const collection = ((_a = target == null ? void 0 : target._collection) == null ? void 0 : _a.collectionName) || ((_b = target == null ? void 0 : target.collection) == null ? void 0 : _b.name) || ((_d = (_c = target == null ? void 0 : target._model) == null ? void 0 : _c.collection) == null ? void 0 : _d.collectionName);
|
|
438
|
-
(_e = target == null ? void 0 : target.logger) == null ? void 0 : _e.operation(op).collection(collection).query(target._conditions).update(target._update).pipeline(target._pipeline).profile().result(res)
|
|
457
|
+
(_e = target == null ? void 0 : target.logger) == null ? void 0 : _e.operation(op).collection(collection).query(target._conditions).update(target._update).pipeline(target._pipeline).profile().result(res);
|
|
458
|
+
if (explained.includes(op)) {
|
|
459
|
+
await (target == null ? void 0 : target.logger.explain(target));
|
|
460
|
+
}
|
|
461
|
+
(_f = target == null ? void 0 : target.logger) == null ? void 0 : _f.info(`DB query: ${op} - ${collection}`);
|
|
439
462
|
}
|
|
440
463
|
function plugin(options = {}) {
|
|
441
464
|
return (schema) => {
|
package/dist/index.umd.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
debug: "white"
|
|
14
14
|
};
|
|
15
15
|
addColors(colors);
|
|
16
|
-
const OPTIONS = {
|
|
16
|
+
const OPTIONS$1 = {
|
|
17
17
|
dirname: "logs",
|
|
18
18
|
levels: {
|
|
19
19
|
error: 0,
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
}
|
|
106
106
|
class Logger {
|
|
107
107
|
constructor(options = {}) {
|
|
108
|
-
this.options = { ...OPTIONS, ...options };
|
|
108
|
+
this.options = { ...OPTIONS$1, ...options };
|
|
109
109
|
if (!this.options.filename.includes("."))
|
|
110
110
|
this.options.filename += ".log";
|
|
111
111
|
if (!this.options.errorFilename.includes("."))
|
|
@@ -245,6 +245,7 @@
|
|
|
245
245
|
}
|
|
246
246
|
class BaseLogger {
|
|
247
247
|
constructor(component, options) {
|
|
248
|
+
this.options = options;
|
|
248
249
|
this.logger = new Logger(options).logger.child({ component });
|
|
249
250
|
this.ctx = {};
|
|
250
251
|
this.startTime = Date.now();
|
|
@@ -395,10 +396,18 @@
|
|
|
395
396
|
"aggregate",
|
|
396
397
|
"save"
|
|
397
398
|
];
|
|
398
|
-
const
|
|
399
|
+
const explained = [
|
|
400
|
+
"find",
|
|
401
|
+
"findOne"
|
|
402
|
+
];
|
|
403
|
+
const OPTIONS = {
|
|
404
|
+
maxResBytes: 100 * 1024,
|
|
405
|
+
logRes: true,
|
|
406
|
+
explain: false
|
|
407
|
+
};
|
|
399
408
|
class MongooseLogger extends BaseLogger {
|
|
400
409
|
constructor(options = {}) {
|
|
401
|
-
super("database", options);
|
|
410
|
+
super("database", { ...OPTIONS, ...options });
|
|
402
411
|
}
|
|
403
412
|
operation(o) {
|
|
404
413
|
this.ctx.operation = o;
|
|
@@ -420,26 +429,40 @@
|
|
|
420
429
|
this.ctx.pipeline = p;
|
|
421
430
|
return this;
|
|
422
431
|
}
|
|
432
|
+
async explain(query) {
|
|
433
|
+
if (this.options.explain && !query.options.explain) {
|
|
434
|
+
this.ctx.explain = await query.clone().explain();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
423
437
|
result(res) {
|
|
424
438
|
try {
|
|
425
439
|
this.ctx.resultSizeBytes = JSON.stringify(res).length;
|
|
440
|
+
this.ctx.resultLength = Array.isArray(res) ? res.length : res ? 1 : 0;
|
|
426
441
|
} catch (err) {
|
|
427
442
|
}
|
|
443
|
+
if (!this.options.logRes)
|
|
444
|
+
return this;
|
|
428
445
|
if (Array.isArray(res)) {
|
|
429
446
|
this.ctx.documentCount = res.length;
|
|
430
|
-
} else if (this.ctx.resultSizeBytes <
|
|
447
|
+
} else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
|
|
431
448
|
this.ctx.result = res;
|
|
432
449
|
} else {
|
|
433
|
-
this.ctx.result = `Result too long (more then ${
|
|
450
|
+
this.ctx.result = `Result too long (more then ${this.options.maxResBytes} bytes)`;
|
|
434
451
|
}
|
|
435
452
|
return this;
|
|
436
453
|
}
|
|
437
454
|
}
|
|
438
|
-
function postHook(target, res) {
|
|
439
|
-
var _a, _b, _c, _d, _e;
|
|
455
|
+
async function postHook(target, res) {
|
|
456
|
+
var _a, _b, _c, _d, _e, _f;
|
|
457
|
+
if (target.options.explain)
|
|
458
|
+
return;
|
|
440
459
|
const op = target.constructor.name === "Aggregate" ? "aggregate" : target.op || target.$op;
|
|
441
460
|
const collection = ((_a = target == null ? void 0 : target._collection) == null ? void 0 : _a.collectionName) || ((_b = target == null ? void 0 : target.collection) == null ? void 0 : _b.name) || ((_d = (_c = target == null ? void 0 : target._model) == null ? void 0 : _c.collection) == null ? void 0 : _d.collectionName);
|
|
442
|
-
(_e = target == null ? void 0 : target.logger) == null ? void 0 : _e.operation(op).collection(collection).query(target._conditions).update(target._update).pipeline(target._pipeline).profile().result(res)
|
|
461
|
+
(_e = target == null ? void 0 : target.logger) == null ? void 0 : _e.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
|
+
(_f = target == null ? void 0 : target.logger) == null ? void 0 : _f.info(`DB query: ${op} - ${collection}`);
|
|
443
466
|
}
|
|
444
467
|
function plugin(options = {}) {
|
|
445
468
|
return (schema) => {
|