zeddemore-logger 1.0.4 → 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');
@@ -251,21 +252,25 @@ 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;
255
+ const _response = axiosError.response;
256
+ if (!_response.request) return;
257
+ const _request = _response.request;
257
258
  const logger = options.logger || ZeddemoreLogger.log(options);
258
259
  if (!logger) return;
259
- const _method = request.method || null;
260
+ const _method = _request.method || null;
260
261
  const method = _method ? chalkHttpVerb(_method) : null;
261
- 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 : '';
262
266
  const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
263
- const status = chalkHttpStatus(response.status);
264
- const _duration = axiosError.duration || response.duration;
267
+ const status = chalkHttpStatus(_response.status);
268
+ const _duration = axiosError.duration || _response.duration;
265
269
  const duration = _duration ? convertMilliseconds(_duration) : '-';
266
- 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;
267
272
  const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
268
- const data = response.data ? JSON.stringify(response.data) : null;
273
+ const data = _data ? JSON.stringify(_data) : null;
269
274
  const parts = [ prefix, method, path, status, duration, contentLength, data ];
270
275
  const message = _.compact(parts).join(' ');
271
276
  logger.error(message);
@@ -275,21 +280,24 @@ class ZeddemoreLogger {
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(' ');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zeddemore-logger",
3
- "version": "1.0.4",
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
  }