zeddemore-logger 1.0.3 → 1.0.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.
@@ -4,6 +4,7 @@ const _ = require('lodash');
4
4
  const chalk = require('chalk');
5
5
  const { createLogger: createWinstonLogger, format, transports } = require('winston');
6
6
  const morgan = require('morgan');
7
+ const sizeof = require('object-sizeof');
7
8
  const { v4: uuid } = require('uuid');
8
9
 
9
10
  const { areHeadersSent, getRequestResponseTime, parseRequest } = require('./http');
@@ -14,7 +15,7 @@ const enums = require('./enums');
14
15
  const { logLevelMap } = require('./mappings/log_mappings');
15
16
 
16
17
  const { colorize, combine, label, metadata, printf, timestamp } = format;
17
- const { morganFormats: mf, httpRequestHeaders: rqh, httpResponseHeaders: rsh, winstonLogLevels: wll, winstonLogLevelNames: wlln } = enums;
18
+ const { morganFormats: mf, httpRequestHeaders: rqh, httpResponseHeaders: rsh, winstonLogLevelNames: wlln, winstonLogLevels: wll } = enums;
18
19
 
19
20
  const COLOR_CONSOLE_WHITE_HEX = '#ECECEC';
20
21
  const DEFAULT_REQUEST_ID_ATTRIBUTE = 'requestId';
@@ -251,45 +252,52 @@ class ZeddemoreLogger {
251
252
  logOptions = logOptions || { };
252
253
  options = options || { };
253
254
  if (!axiosError || !axiosError.response) return;
254
- const response = axiosError.response;
255
- if (!response.request) return;
256
- const request = response.request;
257
- const logLevel = massageLogLevel(logOptions.logLevel);
255
+ const _response = axiosError.response;
256
+ if (!_response.request) return;
257
+ const _request = _response.request;
258
258
  const logger = options.logger || ZeddemoreLogger.log(options);
259
259
  if (!logger) return;
260
- const _method = request.method || null;
260
+ const _method = _request.method || null;
261
261
  const method = _method ? chalkHttpVerb(_method) : null;
262
- const path = response.config.url || '';
262
+ const _config = _response.config || null;
263
+ const _headers = _response.headers || null;
264
+ const _data = _response.data || null;
265
+ const path = _config ? _config.url : '';
263
266
  const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
264
- const status = chalkHttpStatus(response.status);
265
- const _duration = axiosError.duration || response.duration;
267
+ const status = chalkHttpStatus(_response.status);
268
+ const _duration = axiosError.duration || _response.duration;
266
269
  const duration = _duration ? convertMilliseconds(_duration) : '-';
267
- const _contentLength = response.headers ? response.headers[ rsh.CONTENT_LENGTH ] : null;
270
+ let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
271
+ if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
268
272
  const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
269
- const parts = [ prefix, method, path, status, duration, contentLength ];
273
+ const data = _data ? JSON.stringify(_data) : null;
274
+ const parts = [ prefix, method, path, status, duration, contentLength, data ];
270
275
  const message = _.compact(parts).join(' ');
271
- logger[ logLevel ](message);
276
+ logger.error(message);
272
277
  }
273
278
 
274
279
  static writeAxiosResponseLog(axiosResponse, logOptions, options) {
275
280
  logOptions = logOptions || { };
276
281
  options = options || { };
277
282
  if (!axiosResponse || !axiosResponse.request) return;
278
- const request = axiosResponse.request;
283
+ const _request = axiosResponse.request;
279
284
  const axiosConfig = logOptions.axios || { };
280
- const configData = logOptions.data ? JSON.stringify(axiosConfig.data) : null;
285
+ const configData = (logOptions.data && _.isObject(axiosConfig.data)) ? JSON.stringify(axiosConfig.data) : null;
281
286
  const logLevel = massageLogLevel(logOptions.logLevel);
282
287
  const logger = options.logger || ZeddemoreLogger.log(options);
283
288
  if (!logger) return;
284
- const _method = request.method || null;
289
+ const _method = _request.method || null;
285
290
  const method = _method ? chalkHttpVerb(_method) : null;
286
- const path = request.path || '';
291
+ const _headers = axiosResponse.headers || null;
292
+ const _data = axiosResponse.data || null;
293
+ const path = _request.path || '';
287
294
  const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
288
- const responseData = logOptions.response ? JSON.stringify(axiosResponse.data) : null;
295
+ const responseData = (logOptions.response && _.isObject(_data)) ? JSON.stringify(_data) : null;
289
296
  const status = chalkHttpStatus(axiosResponse.status);
290
297
  const _duration = axiosResponse.duration;
291
298
  const duration = _duration ? convertMilliseconds(_duration) : '-';
292
- const _contentLength = axiosResponse.headers ? axiosResponse.headers[ rsh.CONTENT_LENGTH ] : null;
299
+ let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
300
+ if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
293
301
  const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
294
302
  const parts = [ prefix, method, path, configData, status, responseData, duration, contentLength ];
295
303
  const message = _.compact(parts).join(' ');
@@ -301,7 +309,7 @@ class ZeddemoreLogger {
301
309
  module.exports = {
302
310
  log: ZeddemoreLogger.log,
303
311
  winstonLogLevelIdToName,
304
- writeAxiosErrorLog: ZeddemoreLogger.writeAxiosResponseLog,
312
+ writeAxiosErrorLog: ZeddemoreLogger.writeAxiosErrorLog,
305
313
  writeAxiosLog: ZeddemoreLogger.writeAxiosResponseLog, // deprecated
306
314
  writeAxiosResponseLog: ZeddemoreLogger.writeAxiosResponseLog,
307
315
  ZeddemoreLogger,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zeddemore-logger",
3
- "version": "1.0.3",
3
+ "version": "1.0.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",
@@ -23,6 +23,7 @@
23
23
  "chalk": "^4.1.2",
24
24
  "lodash": "^4.17.21",
25
25
  "morgan": "^1.10.0",
26
+ "object-sizeof": "^2.6.5",
26
27
  "uuid": "^10.0.0",
27
28
  "winston": "^3.17.0"
28
29
  }