infront-logger 1.1.0 → 1.1.2

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 CHANGED
@@ -13,7 +13,7 @@ The logger supports both writhing to file and to console.
13
13
  let logger = new BaseLogger("component1", options);
14
14
  logger.log("this is a test message");
15
15
 
16
- ### HttpLogger
16
+ ### HttpLogger (Express Logger)
17
17
  const {HttpLogger} = require('infront-logger');
18
18
 
19
19
  const responseInterceptor = (req, res, next) => {
@@ -103,6 +103,7 @@ The logger supports both writhing to file and to console.
103
103
  app.use(responseInterceptor);
104
104
 
105
105
 
106
+ ### FastifyLogger
106
107
 
107
108
 
108
109
  ### MongooseLoggerPlugin
@@ -132,6 +133,14 @@ Make sure to use before defining schemas;
132
133
  - success(req, res, body)
133
134
  - error(err)
134
135
 
136
+ ### FastifyLogger (extends BaseLogger)
137
+ #### Methods
138
+ - request(req)
139
+ - response(res)
140
+ - body(data)
141
+ - success(req, res, body)
142
+ - error(err)
143
+
135
144
  ## options
136
145
 
137
146
  - dirname - The directory in which the logs will be created (default: "logs")
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("."))
@@ -151,7 +151,7 @@ const redact$2 = (data, ...sensitiveKeysList) => {
151
151
  }
152
152
  const redactedData = {};
153
153
  for (const key in data) {
154
- if (data == null ? void 0 : data.hasOwnProperty(key)) {
154
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
155
155
  if (sensitiveKeysList.includes(key)) {
156
156
  redactedData[key] = "*****";
157
157
  } else {
@@ -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,13 @@ const ops = [
391
392
  "aggregate",
392
393
  "save"
393
394
  ];
394
- const MAX_RESULT_BYTES = 100 * 1024;
395
+ const OPTIONS = {
396
+ maxResBytes: 100 * 1024,
397
+ logRes: true
398
+ };
395
399
  class MongooseLogger extends BaseLogger {
396
400
  constructor(options = {}) {
397
- super("database", options);
401
+ super("database", { ...OPTIONS, ...options });
398
402
  }
399
403
  operation(o) {
400
404
  this.ctx.operation = o;
@@ -421,12 +425,14 @@ class MongooseLogger extends BaseLogger {
421
425
  this.ctx.resultSizeBytes = JSON.stringify(res).length;
422
426
  } catch (err) {
423
427
  }
428
+ if (!this.options.logRes)
429
+ return this;
424
430
  if (Array.isArray(res)) {
425
431
  this.ctx.documentCount = res.length;
426
- } else if (this.ctx.resultSizeBytes < MAX_RESULT_BYTES) {
432
+ } else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
427
433
  this.ctx.result = res;
428
434
  } else {
429
- this.ctx.result = `Result too long (more then ${MAX_RESULT_BYTES} bytes)`;
435
+ this.ctx.result = `Result too long (more then ${this.options.maxResBytes} bytes)`;
430
436
  }
431
437
  return this;
432
438
  }
@@ -465,10 +471,6 @@ class FastifyLogger extends BaseLogger {
465
471
  getResponseHeaders(res) {
466
472
  return res.headers;
467
473
  }
468
- getRequestSocketRemoteAddress(req) {
469
- var _a;
470
- return (_a = req.socket) == null ? void 0 : _a.remoteAddress;
471
- }
472
474
  getRequestUserId(req) {
473
475
  var _a;
474
476
  return (_a = req["user"]) == null ? void 0 : _a.id;
@@ -477,7 +479,7 @@ class FastifyLogger extends BaseLogger {
477
479
  return res.statusCode;
478
480
  }
479
481
  getRequestQuery(req) {
480
- return { ...req.query };
482
+ return req.query;
481
483
  }
482
484
  getRequestParams(req) {
483
485
  return req.params;
@@ -499,7 +501,7 @@ class FastifyLogger extends BaseLogger {
499
501
  return url.origin;
500
502
  }
501
503
  getRequestHeaders(req) {
502
- return req.headers;
504
+ return req.headers || {};
503
505
  }
504
506
  request(req) {
505
507
  this.session(this.getRequestSessionId(req));
@@ -515,7 +517,6 @@ class FastifyLogger extends BaseLogger {
515
517
  return this;
516
518
  }
517
519
  _prepare() {
518
- var _a;
519
520
  const req = this.req;
520
521
  const res = this.res;
521
522
  const body = this.data;
@@ -528,7 +529,7 @@ class FastifyLogger extends BaseLogger {
528
529
  body: redact(this.getRequestBody(req), "password"),
529
530
  params: this.getRequestParams(req),
530
531
  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
+ clientIP: this.getClientIp(req)
532
533
  };
533
534
  this.ctx.response = {
534
535
  headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
@@ -541,7 +542,7 @@ class FastifyLogger extends BaseLogger {
541
542
  }
542
543
  _message(msg) {
543
544
  var _a;
544
- const remoteAddress = this.getRemoteAddress(this.req);
545
+ const remoteAddress = this.getRemoteAddress();
545
546
  const ip = this.ctx.request.clientIP;
546
547
  const method = this.ctx.request.method;
547
548
  const url = this.ctx.request.url;
@@ -550,9 +551,12 @@ class FastifyLogger extends BaseLogger {
550
551
  const responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
551
552
  return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
552
553
  }
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);
554
+ getClientIp(req) {
555
+ var _a, _b;
556
+ 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);
557
+ }
558
+ getRemoteAddress() {
559
+ return this.req.ip || this.req._remoteAddress || void 0;
556
560
  }
557
561
  success(req, res, body) {
558
562
  if (req)
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("."))
@@ -155,7 +155,7 @@
155
155
  }
156
156
  const redactedData = {};
157
157
  for (const key in data) {
158
- if (data == null ? void 0 : data.hasOwnProperty(key)) {
158
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
159
159
  if (sensitiveKeysList.includes(key)) {
160
160
  redactedData[key] = "*****";
161
161
  } else {
@@ -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,13 @@
395
396
  "aggregate",
396
397
  "save"
397
398
  ];
398
- const MAX_RESULT_BYTES = 100 * 1024;
399
+ const OPTIONS = {
400
+ maxResBytes: 100 * 1024,
401
+ logRes: true
402
+ };
399
403
  class MongooseLogger extends BaseLogger {
400
404
  constructor(options = {}) {
401
- super("database", options);
405
+ super("database", { ...OPTIONS, ...options });
402
406
  }
403
407
  operation(o) {
404
408
  this.ctx.operation = o;
@@ -425,12 +429,14 @@
425
429
  this.ctx.resultSizeBytes = JSON.stringify(res).length;
426
430
  } catch (err) {
427
431
  }
432
+ if (!this.options.logRes)
433
+ return this;
428
434
  if (Array.isArray(res)) {
429
435
  this.ctx.documentCount = res.length;
430
- } else if (this.ctx.resultSizeBytes < MAX_RESULT_BYTES) {
436
+ } else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
431
437
  this.ctx.result = res;
432
438
  } else {
433
- this.ctx.result = `Result too long (more then ${MAX_RESULT_BYTES} bytes)`;
439
+ this.ctx.result = `Result too long (more then ${this.options.maxResBytes} bytes)`;
434
440
  }
435
441
  return this;
436
442
  }
@@ -469,10 +475,6 @@
469
475
  getResponseHeaders(res) {
470
476
  return res.headers;
471
477
  }
472
- getRequestSocketRemoteAddress(req) {
473
- var _a;
474
- return (_a = req.socket) == null ? void 0 : _a.remoteAddress;
475
- }
476
478
  getRequestUserId(req) {
477
479
  var _a;
478
480
  return (_a = req["user"]) == null ? void 0 : _a.id;
@@ -481,7 +483,7 @@
481
483
  return res.statusCode;
482
484
  }
483
485
  getRequestQuery(req) {
484
- return { ...req.query };
486
+ return req.query;
485
487
  }
486
488
  getRequestParams(req) {
487
489
  return req.params;
@@ -503,7 +505,7 @@
503
505
  return url.origin;
504
506
  }
505
507
  getRequestHeaders(req) {
506
- return req.headers;
508
+ return req.headers || {};
507
509
  }
508
510
  request(req) {
509
511
  this.session(this.getRequestSessionId(req));
@@ -519,7 +521,6 @@
519
521
  return this;
520
522
  }
521
523
  _prepare() {
522
- var _a;
523
524
  const req = this.req;
524
525
  const res = this.res;
525
526
  const body = this.data;
@@ -532,7 +533,7 @@
532
533
  body: redact(this.getRequestBody(req), "password"),
533
534
  params: this.getRequestParams(req),
534
535
  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
+ clientIP: this.getClientIp(req)
536
537
  };
537
538
  this.ctx.response = {
538
539
  headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
@@ -545,7 +546,7 @@
545
546
  }
546
547
  _message(msg) {
547
548
  var _a;
548
- const remoteAddress = this.getRemoteAddress(this.req);
549
+ const remoteAddress = this.getRemoteAddress();
549
550
  const ip = this.ctx.request.clientIP;
550
551
  const method = this.ctx.request.method;
551
552
  const url = this.ctx.request.url;
@@ -554,9 +555,12 @@
554
555
  const responseSize = formatBytes((_a = JSON.stringify(this.data)) == null ? void 0 : _a.length);
555
556
  return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
556
557
  }
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);
558
+ getClientIp(req) {
559
+ var _a, _b;
560
+ 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);
561
+ }
562
+ getRemoteAddress() {
563
+ return this.req.ip || this.req._remoteAddress || void 0;
560
564
  }
561
565
  success(req, res, body) {
562
566
  if (req)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "infront-logger",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "files": [
6
6
  "dist"
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "winston": "^3.11.0",
28
28
  "winston-daily-rotate-file": "^4.7.1",
29
- "infront-utils": "^1.0.0"
29
+ "infront-utils": "^1.0.1"
30
30
  },
31
31
  "scripts": {
32
32
  "test": "echo \"Error: no test specified\" && exit 1",