Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 8cb00f1

Browse files
authored
Merge pull request #13 from afrisalyp/issue-logging-new
New logging, disable eventdecorator logs.
2 parents 148fa03 + cc79a4f commit 8cb00f1

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

app.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
'use strict';
1111
const express = require('express');
12-
// const logger = require('morgan');
1312
const cookieParser = require('cookie-parser');
1413
const bodyParser = require('body-parser');
1514
const healthcheck = require('topcoder-healthcheck-dropin');
@@ -61,20 +60,16 @@ app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
6160
});
6261

6362
process.on('uncaughtException', (err) => {
64-
// logger.error('Exception: ', err);
6563
// Check if error related to Dynamodb conn
6664
if (err.code === 'NetworkingError' && err.region) {
6765
logger.error('DynamoDB connection failed.');
6866
}
6967
logger.logFullError(err, 'system');
70-
// console.log(err);
7168
});
7269

7370
// handle and log unhanled rejection
7471
process.on('unhandledRejection', (err) => {
75-
// logger.error('Rejection: ', err);
7672
logger.logFullError(err, 'system');
77-
// console.log(err);
7873
});
7974

8075
module.exports = app;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
"debug": "^2.6.9",
1717
"dynamoose": "^1.1.0",
1818
"express": "^4.17.1",
19+
"get-parameter-names": "^0.3.0",
20+
"global-request-logger": "^0.1.1",
1921
"joi": "^11.4.0",
20-
"morgan": "^1.9.1",
2122
"no-kafka": "^3.4.3",
2223
"topcoder-healthcheck-dropin": "^1.0.3",
23-
"winston": "^2.4.2"
24+
"winston": "^2.4.3"
2425
},
2526
"devDependencies": {
2627
"eslint-config-topcoder": "^2.0.0",

utils/logger.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
*/
1111
'use strict';
1212
const config = require('config');
13+
const util = require('util');
14+
const _ = require('lodash');
1315
const winston = require('winston');
16+
const getParams = require('get-parameter-names');
17+
const globalLog = require('global-request-logger');
1418

1519
const logger = new winston.Logger({
1620
transports: [
@@ -33,5 +37,94 @@ logger.logFullError = function logFullError(err, signature) {
3337
err.logged = true;
3438
};
3539

40+
/**
41+
* Remove invalid properties from the object and hide long arrays
42+
* @param {Object} obj the object
43+
* @returns {Object} the new object with removed properties
44+
* @private
45+
*/
46+
function sanitizeObject(obj) {
47+
try {
48+
return JSON.parse(JSON.stringify(obj, (name, value) => {
49+
// Array of field names that should not be logged
50+
const removeFields = ['refreshToken', 'accessToken', 'access_token', 'authorization'];
51+
if (_.includes(removeFields, name)) {
52+
return '<removed>';
53+
}
54+
if (_.isArray(value) && value.length > 30) { // eslint-disable-line
55+
return `Array(${value.length}`;
56+
}
57+
return value;
58+
}));
59+
} catch (e) {
60+
return obj;
61+
}
62+
}
63+
64+
/**
65+
* Convert array with arguments to object
66+
* @param {Array} params the name of parameters
67+
* @param {Array} arr the array with values
68+
* @returns {Object} converted object
69+
* @private
70+
*/
71+
function combineObject(params, arr) {
72+
const ret = {};
73+
_.forEach(arr, (arg, i) => {
74+
ret[params[i]] = arg;
75+
});
76+
return ret;
77+
}
78+
79+
/**
80+
* Decorate all functions of a service and log debug information if DEBUG is enabled
81+
* @param {Object} service the service
82+
*/
83+
logger.decorateWithLogging = function decorateWithLogging(service) {
84+
if (config.LOG_LEVEL !== 'debug') {
85+
return;
86+
}
87+
_.forEach(service, (method, name) => {
88+
const params = method.params || getParams(method);
89+
service[name] = async function serviceMethodWithLogging() {
90+
logger.debug(`ENTER ${name}`);
91+
logger.debug('input arguments');
92+
const args = Array.prototype.slice.call(arguments); // eslint-disable-line
93+
logger.debug(util.inspect(sanitizeObject(combineObject(params, args))));
94+
try {
95+
const result = await method.apply(this, arguments); // eslint-disable-line
96+
logger.debug(`EXIT ${name}`);
97+
logger.debug('output arguments');
98+
logger.debug(util.inspect(sanitizeObject(result)));
99+
return result;
100+
} catch (e) {
101+
logger.logFullError(e, name);
102+
throw e;
103+
}
104+
};
105+
});
106+
};
107+
108+
/**
109+
* Apply logger and validation decorators
110+
* @param {Object} service the service to wrap
111+
*/
112+
logger.buildService = function buildService(service) {
113+
logger.decorateWithLogging(service);
114+
};
115+
116+
// globalLog.initialize();
117+
118+
// global any http success request interceptor
119+
globalLog.on('success', (request, response) => {
120+
logger.debug('Request', util.inspect(sanitizeObject(request)));
121+
logger.debug('Response', util.inspect(sanitizeObject(response)));
122+
});
123+
124+
// global any http error request interceptor
125+
globalLog.on('error', (request, response) => {
126+
logger.error('Request', util.inspect(sanitizeObject(request)));
127+
logger.error('Response', util.inspect(sanitizeObject(response)));
128+
});
36129

37130
module.exports = logger;

0 commit comments

Comments
 (0)