zeddemore-logger 1.2.3 → 1.2.5

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.
@@ -20,8 +20,10 @@ const { morganFormats: mf, morganFormatTokens: mft, httpMethods: http, httpReque
20
20
  winstonLogLevelNames: wlln, winstonLogLevels: wll } = enums;
21
21
 
22
22
  const COLOR_CONSOLE_WHITE_HEX = '#ECECEC';
23
+ const DEFAULT_CONTENT_LENGTH_DIGITS = 2;
23
24
  const DEFAULT_MORGAN_FORMAT_TOKENS = [ mft.METHOD, mft.DECODED_URL, mft.STATUS_COLORED, mft.RESPONSE_TIME_FORMAT, mft.CONTENT_LENGTH_FORMAT ];
24
25
  const DEFAULT_REQUEST_ID_ATTRIBUTE = 'requestId';
26
+ const DEFAULT_RESPONSE_TIME_DIGITS = 0;
25
27
  const DEFAULT_USER_LOG_LEVEL = -1;
26
28
  const WINSTON_CONSOLE_TRANSPORT_NAME = 'console';
27
29
 
@@ -67,6 +69,7 @@ class ZeddemoreLogger {
67
69
  this.apiRouteCategories = options.apiRouteCategories || [ ];
68
70
  this.apiRoutes = options.apiRoutes || [ ];
69
71
  this.apiVersion = options.apiVersion || null;
72
+ this.contentLengthDigits = _.isInteger(options.contentLengthDigits) ? options.contentLengthDigits : DEFAULT_CONTENT_LENGTH_DIGITS;
70
73
  this.decodeUrls = _.isBoolean(options.decodeUrls) ? options.decodeUrls : true;
71
74
  this.defaultLabel = options.defaultLabel || 'zeddemore-logger';
72
75
  this.disableApiCallLogging = _.isBoolean(options.disableApiCallLogging) ? options.disableApiCallLogging : false;
@@ -80,6 +83,7 @@ class ZeddemoreLogger {
80
83
  this.#forceLogLevel = _.isBoolean(options.forceLogLevel) ? options.forceLogLevel : false;
81
84
  this.#logLevel = _.isInteger(options.logLevel) ? options.logLevel : wll.INFO;
82
85
  this.morganFormat = _.isString(options.morganFormat) ? options.morganFormat : null;
86
+ this.responseTimeDigits = _.isInteger(options.responseTimeDigits) ? options.responseTimeDigits : DEFAULT_RESPONSE_TIME_DIGITS;
83
87
  this.routeModel = options.routeModel || null;
84
88
  this.suppressNonMutatingRequestApiCallLogging = _.isBoolean(options.suppressNonMutatingRequestApiCallLogging) ? options.suppressNonMutatingRequestApiCallLogging : false;
85
89
  this.suppressSuccessfulRequestApiCallLogging = _.isBoolean(options.suppressSuccessfulRequestApiCallLogging) ? options.suppressSuccessfulRequestApiCallLogging : false;
@@ -142,17 +146,15 @@ class ZeddemoreLogger {
142
146
  const closeBracketIndex = token.indexOf(']');
143
147
  return ((openBracketIndex > -1) && (closeBracketIndex > -1) && (closeBracketIndex > (openBracketIndex + 1)));
144
148
  }
145
- const DEFAULT_CONTENT_LENGTH_FORMAT_DIGITS = 0;
146
- const DEFAULT_RESPONSE_TIME_DIGITS = 2;
147
149
  if (!morganTokens || !_.isArray(morganTokens) || !morganTokens.length) return mf.DEV;
148
150
  const _morganTokens = _.map(morganTokens, (token) => {
149
151
  if (!_.isString(token) || !token.length) return null;
150
152
  let _token = token.trim();
151
153
  if (_token[ 0 ] !== ':') _token = `:${ _token }`;
152
154
  switch (_token) {
153
- case mft.CONTENT_LENGTH_FORMAT: return hasArgument(_token) ? _token : addArgument(_token, DEFAULT_CONTENT_LENGTH_FORMAT_DIGITS);
155
+ case mft.CONTENT_LENGTH_FORMAT: return hasArgument(_token) ? _token : addArgument(_token, this.contentLengthDigits);
154
156
  case mft.RESPONSE_TIME:
155
- case mft.RESPONSE_TIME_FORMAT: return hasArgument(_token) ? _token : addArgument(_token, DEFAULT_RESPONSE_TIME_DIGITS);
157
+ case mft.RESPONSE_TIME_FORMAT: return hasArgument(_token) ? _token : addArgument(_token, this.responseTimeDigits);
156
158
  default: return _token;
157
159
  }
158
160
  });
@@ -283,15 +285,15 @@ class ZeddemoreLogger {
283
285
  }
284
286
 
285
287
  #initializeMorgan() {
286
- function buildContentLengthFormatToken(res, digits = 2) {
287
- const contentLength = convertBytes(res.get('content-length'), { digits });
288
+ function buildContentLengthFormatToken(res, decimals = DEFAULT_CONTENT_LENGTH_DIGITS) {
289
+ const contentLength = convertBytes(res.get('content-length'), { decimals });
288
290
  return contentLength ? ` - ${ contentLength }` : '';
289
291
  }
290
292
  function buildDecodedUrl(req) {
291
293
  const url = req.originalUrl || req.url;
292
294
  return decodeURI(url);
293
295
  }
294
- function buildResponseTimeFormatToken(req, res, digits = 0) {
296
+ function buildResponseTimeFormatToken(req, res, digits = DEFAULT_RESPONSE_TIME_DIGITS) {
295
297
  const responseTime = getRequestResponseTime(req, res);
296
298
  return responseTime ? convertMilliseconds(responseTime, { digits }) : '-';
297
299
  }
@@ -300,7 +302,7 @@ class ZeddemoreLogger {
300
302
  if (!statusCode) return '-';
301
303
  return chalkHttpStatuses(statusCode);
302
304
  }
303
- morgan.token(mft.CONTENT_LENGTH_FORMAT, (req, res, digits) => { return buildContentLengthFormatToken(res, digits); });
305
+ morgan.token(mft.CONTENT_LENGTH_FORMAT, (req, res, decimals) => { return buildContentLengthFormatToken(res, decimals); });
304
306
  morgan.token(mft.DECODED_URL, (req, res) => { return buildDecodedUrl(req); });
305
307
  morgan.token(mft.RESPONSE_TIME_FORMAT, (req, res, digits) => { return buildResponseTimeFormatToken(req, res, digits); });
306
308
  morgan.token(mft.STATUS_COLORED, (req, res) => { return buildStatusColoredToken(res); });
@@ -537,10 +539,10 @@ class ZeddemoreLogger {
537
539
  const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
538
540
  const status = chalkHttpStatuses(_response.status);
539
541
  const _duration = axiosError.duration || _response.duration;
540
- const duration = _duration ? convertMilliseconds(_duration) : '-';
542
+ const duration = _duration ? convertMilliseconds(_duration, logger.responseTimeDigits) : '-';
541
543
  let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
542
544
  if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
543
- const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
545
+ const contentLength = _contentLength ? `- ${ convertBytes(_contentLength, logger.contentLengthDigits) }` : null;
544
546
  const data = _data ? JSON.stringify(_data) : null;
545
547
  const parts = [ prefix, method, path, status, duration, contentLength, data ];
546
548
  const message = _.compact(parts).join(' ');
@@ -566,10 +568,11 @@ class ZeddemoreLogger {
566
568
  const responseData = (logOptions.response && _.isObject(_data)) ? JSON.stringify(_data) : null;
567
569
  const status = chalkHttpStatuses(axiosResponse.status);
568
570
  const _duration = axiosResponse.duration;
569
- const duration = _duration ? convertMilliseconds(_duration) : '-';
571
+ const duration = _duration ? convertMilliseconds(_duration, logger.responseTimeDigits) : '-';
570
572
  let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
571
573
  if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
572
- const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
574
+ const contentLengthDigits = logOptions.contentLengthDigits || logger.contentLengthDigits || DEFAULT_CONTENT_LENGTH_DIGITS;
575
+ const contentLength = _contentLength ? `- ${ convertBytes(_contentLength, contentLengthDigits) }` : null;
573
576
  const parts = [ prefix, method, path, configData, status, responseData, duration, contentLength ];
574
577
  const message = _.compact(parts).join(' ');
575
578
  logger[ logLevel ](message);
@@ -578,6 +581,7 @@ class ZeddemoreLogger {
578
581
  }
579
582
 
580
583
  module.exports = {
584
+ enums,
581
585
  log: ZeddemoreLogger.getLogger,
582
586
  winstonLogLevelIdToName,
583
587
  writeAxiosErrorLog: ZeddemoreLogger.writeAxiosErrorLog,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zeddemore-logger",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Node.js Express logger using Morgan and Winston for thread and caller info tracking",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,7 @@
22
22
  "dependencies": {
23
23
  "chalk": "^4.1.2",
24
24
  "lodash": "^4.17.21",
25
- "morgan": "^1.10.0",
25
+ "morgan": "^1.10.1",
26
26
  "object-sizeof": "^2.6.5",
27
27
  "uuid": "^10.0.0",
28
28
  "winston": "^3.17.0"