zeddemore-logger 1.0.2 → 1.0.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.
@@ -21,6 +21,17 @@ const DEFAULT_REQUEST_ID_ATTRIBUTE = 'requestId';
21
21
  const DEFAULT_USER_LOG_LEVEL = -1;
22
22
  const WINSTON_CONSOLE_TRANSPORT_NAME = 'console';
23
23
 
24
+ function massageLogLevel(logLevel) {
25
+ if (!logLevel) return wlln.VERBOSE;
26
+ if (!_.isInteger(logLevel)) return logLevel;
27
+ return winstonLogLevelIdToName(logLevel);
28
+ }
29
+
30
+ function winstonLogLevelIdToName(levelId) {
31
+ const logLevelEntry = _.find(logLevelMap, { winstonLevel: levelId });
32
+ return logLevelEntry ? logLevelEntry.winstonName : wlln.HTTP;
33
+ }
34
+
24
35
  class ZeddemoreLogger {
25
36
 
26
37
  constructor(options) {
@@ -62,7 +73,7 @@ class ZeddemoreLogger {
62
73
 
63
74
  #buildWinstonTransport = (logLevel = this.logLevel) => {
64
75
  logLevel = logLevel || this.logLevel;
65
- const level = ZeddemoreLogger.winstonLogLevelIdToName(logLevel);
76
+ const level = winstonLogLevelIdToName(logLevel);
66
77
  return new transports.Console({ level, name: WINSTON_CONSOLE_TRANSPORT_NAME });
67
78
  }
68
79
 
@@ -236,28 +247,43 @@ class ZeddemoreLogger {
236
247
  return next();
237
248
  }
238
249
 
239
- static winstonLogLevelIdToName(levelId) {
240
- const logLevelEntry = _.find(logLevelMap, { winstonLevel: levelId });
241
- return logLevelEntry ? logLevelEntry.winstonName : wlln.HTTP;
250
+ static writeAxiosErrorLog(axiosError, logOptions, options) {
251
+ logOptions = logOptions || { };
252
+ options = options || { };
253
+ 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);
258
+ const logger = options.logger || ZeddemoreLogger.log(options);
259
+ if (!logger) return;
260
+ const _method = request.method || null;
261
+ const method = _method ? chalkHttpVerb(_method) : null;
262
+ const path = response.config.url || '';
263
+ const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
264
+ const status = chalkHttpStatus(response.status);
265
+ const _duration = axiosError.duration || response.duration;
266
+ const duration = _duration ? convertMilliseconds(_duration) : '-';
267
+ const _contentLength = response.headers ? response.headers[ rsh.CONTENT_LENGTH ] : null;
268
+ const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
269
+ const parts = [ prefix, method, path, status, duration, contentLength ];
270
+ const message = _.compact(parts).join(' ');
271
+ logger[ logLevel ](message);
242
272
  }
243
273
 
244
- static writeAxiosLog(axiosResponse, logOptions, options) {
245
- function massageLogLevel(logLevel) {
246
- if (!logLevel) return wlln.VERBOSE;
247
- if (!_.isInteger(logLevel)) return logLevel;
248
- return ZeddemoreLogger.winstonLogLevelIdToName(logLevel);
249
- }
250
- if (!axiosResponse || !axiosResponse.request) return;
274
+ static writeAxiosResponseLog(axiosResponse, logOptions, options) {
251
275
  logOptions = logOptions || { };
252
276
  options = options || { };
277
+ if (!axiosResponse || !axiosResponse.request) return;
278
+ const request = axiosResponse.request;
253
279
  const axiosConfig = logOptions.axios || { };
254
280
  const configData = logOptions.data ? JSON.stringify(axiosConfig.data) : null;
255
281
  const logLevel = massageLogLevel(logOptions.logLevel);
256
282
  const logger = options.logger || ZeddemoreLogger.log(options);
257
283
  if (!logger) return;
258
- const _method = axiosResponse.request.method || null;
284
+ const _method = request.method || null;
259
285
  const method = _method ? chalkHttpVerb(_method) : null;
260
- const path = axiosResponse.request.path || '';
286
+ const path = request.path || '';
261
287
  const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
262
288
  const responseData = logOptions.response ? JSON.stringify(axiosResponse.data) : null;
263
289
  const status = chalkHttpStatus(axiosResponse.status);
@@ -274,7 +300,9 @@ class ZeddemoreLogger {
274
300
 
275
301
  module.exports = {
276
302
  log: ZeddemoreLogger.log,
303
+ winstonLogLevelIdToName,
304
+ writeAxiosErrorLog: ZeddemoreLogger.writeAxiosResponseLog,
305
+ writeAxiosLog: ZeddemoreLogger.writeAxiosResponseLog, // deprecated
306
+ writeAxiosResponseLog: ZeddemoreLogger.writeAxiosResponseLog,
277
307
  ZeddemoreLogger,
278
- winstonLogLevelIdToName: ZeddemoreLogger.winstonLogLevelIdToName,
279
- writeAxiosLog: ZeddemoreLogger.writeAxiosLog,
280
308
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zeddemore-logger",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Node.js Express logger using Morgan and Winston for thread and caller info tracking",
5
5
  "repository": {
6
6
  "type": "git",