zeddemore-logger 1.0.2 → 1.0.4
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/lib/zeddemore-logger.js +44 -16
- package/package.json +1 -1
package/lib/zeddemore-logger.js
CHANGED
|
@@ -14,13 +14,24 @@ const enums = require('./enums');
|
|
|
14
14
|
const { logLevelMap } = require('./mappings/log_mappings');
|
|
15
15
|
|
|
16
16
|
const { colorize, combine, label, metadata, printf, timestamp } = format;
|
|
17
|
-
const { morganFormats: mf, httpRequestHeaders: rqh, httpResponseHeaders: rsh,
|
|
17
|
+
const { morganFormats: mf, httpRequestHeaders: rqh, httpResponseHeaders: rsh, winstonLogLevelNames: wlln, winstonLogLevels: wll } = enums;
|
|
18
18
|
|
|
19
19
|
const COLOR_CONSOLE_WHITE_HEX = '#ECECEC';
|
|
20
20
|
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 =
|
|
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
|
|
240
|
-
|
|
241
|
-
|
|
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 logger = options.logger || ZeddemoreLogger.log(options);
|
|
258
|
+
if (!logger) return;
|
|
259
|
+
const _method = request.method || null;
|
|
260
|
+
const method = _method ? chalkHttpVerb(_method) : null;
|
|
261
|
+
const path = response.config.url || '';
|
|
262
|
+
const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
|
|
263
|
+
const status = chalkHttpStatus(response.status);
|
|
264
|
+
const _duration = axiosError.duration || response.duration;
|
|
265
|
+
const duration = _duration ? convertMilliseconds(_duration) : '-';
|
|
266
|
+
const _contentLength = response.headers ? response.headers[ rsh.CONTENT_LENGTH ] : null;
|
|
267
|
+
const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
|
|
268
|
+
const data = response.data ? JSON.stringify(response.data) : null;
|
|
269
|
+
const parts = [ prefix, method, path, status, duration, contentLength, data ];
|
|
270
|
+
const message = _.compact(parts).join(' ');
|
|
271
|
+
logger.error(message);
|
|
242
272
|
}
|
|
243
273
|
|
|
244
|
-
static
|
|
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 =
|
|
284
|
+
const _method = request.method || null;
|
|
259
285
|
const method = _method ? chalkHttpVerb(_method) : null;
|
|
260
|
-
const 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.writeAxiosErrorLog,
|
|
305
|
+
writeAxiosLog: ZeddemoreLogger.writeAxiosResponseLog, // deprecated
|
|
306
|
+
writeAxiosResponseLog: ZeddemoreLogger.writeAxiosResponseLog,
|
|
277
307
|
ZeddemoreLogger,
|
|
278
|
-
winstonLogLevelIdToName: ZeddemoreLogger.winstonLogLevelIdToName,
|
|
279
|
-
writeAxiosLog: ZeddemoreLogger.writeAxiosLog,
|
|
280
308
|
};
|