zeddemore-logger 1.0.4 → 1.0.6
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 +30 -18
- package/package.json +2 -1
package/lib/zeddemore-logger.js
CHANGED
|
@@ -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');
|
|
@@ -216,22 +217,25 @@ class ZeddemoreLogger {
|
|
|
216
217
|
|
|
217
218
|
requestLoggerMiddleware = (req, res, next) => {
|
|
218
219
|
const requestValues = parseRequest(req);
|
|
219
|
-
const requestId =
|
|
220
|
+
const requestId = this.#generateUuid();
|
|
220
221
|
req[ this.requestIdAttribute ] = requestId;
|
|
221
222
|
res.header(rqh.REQUEST_ID, requestId);
|
|
222
223
|
const ipAddress = requestValues.ipAddress;
|
|
224
|
+
res.locals = res.locals || { };
|
|
223
225
|
res.locals.user = res.locals.user || { };
|
|
224
226
|
res.locals.user.logger = this.createRequestLogger(requestId, ipAddress);
|
|
225
227
|
return next();
|
|
226
228
|
}
|
|
227
229
|
|
|
228
230
|
#userGetFn(req, res) {
|
|
229
|
-
return (res
|
|
231
|
+
return ((res && res.locals) ? res.locals.user : null)
|
|
232
|
+
|| ((req && req.locals) ? req.locals.user : null)
|
|
233
|
+
|| (req ? req.user : null);
|
|
230
234
|
}
|
|
231
235
|
|
|
232
236
|
userLoggerMiddleware = (req, res, next) => {
|
|
233
237
|
const requestId = this.#getRequestId(req);
|
|
234
|
-
const user =
|
|
238
|
+
const user = this.userGetFn(req, res) || { };
|
|
235
239
|
if (!requestId) return next();
|
|
236
240
|
const username = user.login || null;
|
|
237
241
|
let ipAddress = user.ipAddress || null;
|
|
@@ -242,6 +246,7 @@ class ZeddemoreLogger {
|
|
|
242
246
|
let userLogLevel = !_.isNil(user.logLevel) ? user.logLevel : this.defaultUserLogLevel;
|
|
243
247
|
if (userLogLevel < 0) userLogLevel = this.logLevel;
|
|
244
248
|
const level = this.forceLogLevel ? this.logLevel : userLogLevel;
|
|
249
|
+
res.locals = res.locals || { };
|
|
245
250
|
res.locals.user = res.locals.user || user;
|
|
246
251
|
res.locals.user.logger = this.createUserLogger(requestId, username, level, ipAddress);
|
|
247
252
|
return next();
|
|
@@ -251,21 +256,25 @@ class ZeddemoreLogger {
|
|
|
251
256
|
logOptions = logOptions || { };
|
|
252
257
|
options = options || { };
|
|
253
258
|
if (!axiosError || !axiosError.response) return;
|
|
254
|
-
const
|
|
255
|
-
if (!
|
|
256
|
-
const
|
|
259
|
+
const _response = axiosError.response;
|
|
260
|
+
if (!_response.request) return;
|
|
261
|
+
const _request = _response.request;
|
|
257
262
|
const logger = options.logger || ZeddemoreLogger.log(options);
|
|
258
263
|
if (!logger) return;
|
|
259
|
-
const _method =
|
|
264
|
+
const _method = _request.method || null;
|
|
260
265
|
const method = _method ? chalkHttpVerb(_method) : null;
|
|
261
|
-
const
|
|
266
|
+
const _config = _response.config || null;
|
|
267
|
+
const _headers = _response.headers || null;
|
|
268
|
+
const _data = _response.data || null;
|
|
269
|
+
const path = _config ? _config.url : '';
|
|
262
270
|
const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
|
|
263
|
-
const status = chalkHttpStatus(
|
|
264
|
-
const _duration = axiosError.duration ||
|
|
271
|
+
const status = chalkHttpStatus(_response.status);
|
|
272
|
+
const _duration = axiosError.duration || _response.duration;
|
|
265
273
|
const duration = _duration ? convertMilliseconds(_duration) : '-';
|
|
266
|
-
|
|
274
|
+
let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
|
|
275
|
+
if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
|
|
267
276
|
const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
|
|
268
|
-
const data =
|
|
277
|
+
const data = _data ? JSON.stringify(_data) : null;
|
|
269
278
|
const parts = [ prefix, method, path, status, duration, contentLength, data ];
|
|
270
279
|
const message = _.compact(parts).join(' ');
|
|
271
280
|
logger.error(message);
|
|
@@ -275,21 +284,24 @@ class ZeddemoreLogger {
|
|
|
275
284
|
logOptions = logOptions || { };
|
|
276
285
|
options = options || { };
|
|
277
286
|
if (!axiosResponse || !axiosResponse.request) return;
|
|
278
|
-
const
|
|
287
|
+
const _request = axiosResponse.request;
|
|
279
288
|
const axiosConfig = logOptions.axios || { };
|
|
280
|
-
const configData = logOptions.data ? JSON.stringify(axiosConfig.data) : null;
|
|
289
|
+
const configData = (logOptions.data && _.isObject(axiosConfig.data)) ? JSON.stringify(axiosConfig.data) : null;
|
|
281
290
|
const logLevel = massageLogLevel(logOptions.logLevel);
|
|
282
291
|
const logger = options.logger || ZeddemoreLogger.log(options);
|
|
283
292
|
if (!logger) return;
|
|
284
|
-
const _method =
|
|
293
|
+
const _method = _request.method || null;
|
|
285
294
|
const method = _method ? chalkHttpVerb(_method) : null;
|
|
286
|
-
const
|
|
295
|
+
const _headers = axiosResponse.headers || null;
|
|
296
|
+
const _data = axiosResponse.data || null;
|
|
297
|
+
const path = _request.path || '';
|
|
287
298
|
const prefix = logOptions.prefix ? `[${ logOptions.prefix }]` : null;
|
|
288
|
-
const responseData = logOptions.response ? JSON.stringify(
|
|
299
|
+
const responseData = (logOptions.response && _.isObject(_data)) ? JSON.stringify(_data) : null;
|
|
289
300
|
const status = chalkHttpStatus(axiosResponse.status);
|
|
290
301
|
const _duration = axiosResponse.duration;
|
|
291
302
|
const duration = _duration ? convertMilliseconds(_duration) : '-';
|
|
292
|
-
|
|
303
|
+
let _contentLength = _headers ? _headers[ rsh.CONTENT_LENGTH ] : null;
|
|
304
|
+
if (!_contentLength) _contentLength = _data ? sizeof(_data) : null;
|
|
293
305
|
const contentLength = _contentLength ? `- ${ convertBytes(_contentLength) }` : null;
|
|
294
306
|
const parts = [ prefix, method, path, configData, status, responseData, duration, contentLength ];
|
|
295
307
|
const message = _.compact(parts).join(' ');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zeddemore-logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
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
|
}
|