Skip to content

Commit

Permalink
chore: bump logger and change logger statements for valid json (#153)
Browse files Browse the repository at this point in the history
* chore: bump logger and change logger statements

* chore: dep update
  • Loading branch information
kleyow committed Nov 22, 2022
1 parent 4789909 commit 6583dec
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 57 deletions.
30 changes: 16 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"dependencies": {
"@koa/cors": "4.0.0",
"@mojaloop/central-services-logger": "11.1.0",
"@mojaloop/central-services-logger": "^11.2.0",
"ajv": "8.11.2",
"chance": "1.1.9",
"core-util-is": "1.0.3",
Expand Down Expand Up @@ -79,7 +79,7 @@
"eslint-plugin-jest": "^27.1.5",
"husky": "8.0.2",
"jest": "^29.3.1",
"jest-junit": "^14.0.1",
"jest-junit": "^15.0.0",
"npm-audit-resolver": "^3.0.0-0",
"npm-check-updates": "16.4.1",
"npm-run-all": "4.1.5",
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const Koa = require('koa');
const koaBody = require('koa-body').default;
const { generateSlug } = require('random-word-slugs');
const yaml = require('yamljs');
const util = require('util');
const https = require('https');
const cors = require('@koa/cors');
const router = require('./lib/router');
Expand Down Expand Up @@ -289,9 +288,9 @@ async function rewriteContentTypeHeader(ctx, next) {
method: ctx.request.method,
};
if (ctx.path == '/' || ctx.path == '/health') {
ctx.state.logger.isDebugEnabled && ctx.state.logger.debug(`Rules engine evaluating request against facts: ${util.inspect(facts)}`);
ctx.state.logger.isDebugEnabled && ctx.state.logger.debug({'msg':'Rules engine evaluating request against facts', facts});
} else {
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`Rules engine evaluating request against facts: ${util.inspect(facts)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({'msg':'Rules engine evaluating request against facts', facts});
}

const res = await rulesEngine.evaluate(facts);
Expand Down Expand Up @@ -336,7 +335,7 @@ async function rewriteContentTypeHeader(ctx, next) {
}

const { body, statusCode } = res[0];
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`Rules engine returned a response for request: ${util.inspect(res)}.`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({'msg': 'Rules engine returned a response for request', res});
ctx.response.body = body;
ctx.response.status = statusCode;
return;
Expand Down
13 changes: 6 additions & 7 deletions src/lib/rules-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
******/
'use strict';

const util = require('util');
const { Engine } = require('json-rules-engine');
const { getStackOrInspect } = require('./log/log');

Expand Down Expand Up @@ -52,7 +51,7 @@ class RulesEngine {
loadRules(rules) {
try {
rules.forEach((r) => { this.engine.addRule(r); });
this.logger.isInfoEnabled && this.logger.info(`Rules loaded: ${util.inspect(rules, { depth: 20 })}`);
this.logger.isInfoEnabled && this.logger.info({msg: 'Rules loaded', rules});
} catch (err) {
this.logger.isErrorEnabled && this.logger.error(`Error loading rules: ${getStackOrInspect(err)}`);
throw err;
Expand All @@ -63,24 +62,24 @@ class RulesEngine {
* Runs the engine to evaluate facts
*
* @async
* @param {Object} facts facts to evalute
* @param {Object} facts facts to evaluate
* @returns {Promise.<Object>} response
*/
async evaluate(facts) {
return new Promise((resolve, reject) => {
if (facts.path == '/' || facts.path == '/health') {
this.logger.isDebugEnabled && this.logger.debug(`Rule engine evaluating facts: ${util.inspect(facts)}`);
this.logger.isDebugEnabled && this.logger.debug({ msg: 'Rule engine evaluating facts', facts});
} else {
this.logger.isInfoEnabled && this.logger.info(`Rule engine evaluating facts: ${util.inspect(facts)}`);
this.logger.isInfoEnabled && this.logger.info({ msg: 'Rule engine evaluating facts', facts});
}
this.engine
.run(facts)
.then((engineResult) => {
const { events } = engineResult;
if (facts.path == '/' || facts.path == '/health') {
this.logger.isDebugEnabled && this.logger.debug(`Rule engine returning events: ${util.inspect(engineResult)}`);
this.logger.isDebugEnabled && this.logger.debug({ msg: 'Rule engine returning events', engineResult});
} else {
this.logger.isInfoEnabled && this.logger.info(`Rule engine returning events: ${util.inspect(engineResult)}`);
this.logger.isInfoEnabled && this.logger.info({ msg: 'Rule engine returning events', engineResult});
}
// Events is always longer than 0 for istanbul
/* istanbul ignore next */
Expand Down
2 changes: 0 additions & 2 deletions src/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ const transformApiDoc = (apiDoc) => ({
return {
[method]: {
validator: (ctx, path) => {
// console.log(util.inspect(validatorF.schema,
// { depth: Infinity }));
const result = validatorF({
body: ctx.request.body,
headers: ctx.request.headers,
Expand Down
49 changes: 24 additions & 25 deletions src/simulator/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
******/
'use strict';

const util = require('util');
const crypto = require('crypto');

const { getStackOrInspect } = require('../lib/log/log');
Expand All @@ -41,7 +40,7 @@ const getParticipantsByTypeAndId = async (ctx) => {
ctx.response.status = 404;
return;
}
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getParticipantsByTypeAndId is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'getParticipantsByTypeAndId is returning body', body: res});
ctx.response.body = { fspId: process.env.DFSP_ID };
ctx.response.status = 200;
} catch (err) {
Expand All @@ -61,7 +60,7 @@ const getPartiesByTypeAndId = async (ctx) => {
ctx.response.status = 404;
return;
}
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getPartiesByTypeAndId is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'getPartiesByTypeAndId is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -75,15 +74,15 @@ const getOTPById = async (ctx) => {
const res = {
otpValue: Math.floor(Math.random() * 90000) + 10000,
};
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getOTPById is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'getOTPById is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};

const postTransfers = async (ctx) => {
try {
const res = await ctx.state.model.transfer.create(ctx.request.body);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postTransfers is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'postTransfers is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -98,7 +97,7 @@ const putTransfersById = async (ctx) => {
const res = await ctx.state.model.transfer.update(ctx.state.path.params.transferId, {
...ctx.request.body,
});
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`putTransfersById is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'TransfersById is returning body', body: res});
ctx.response.body = ctx.request.body;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -111,7 +110,7 @@ const putTransfersById = async (ctx) => {
const postQuotes = async (ctx) => {
try {
const res = await ctx.state.model.quote.create(ctx.request.body);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postQuotes is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'Quotes is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -124,7 +123,7 @@ const postQuotes = async (ctx) => {
const postBulkQuotes = async (ctx) => {
try {
const res = await ctx.state.model.bulkQuote.create(ctx.request.body);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postBulkQuotes is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'BulkQuotes is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -143,7 +142,7 @@ const getBulkQuoteById = async (ctx) => {
ctx.response.status = 404;
return;
}
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getBulkQuoteById is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'BulkQuoteById is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -156,7 +155,7 @@ const getBulkQuoteById = async (ctx) => {
const postTransactionRequests = async (ctx) => {
try {
const res = await ctx.state.model.transactionrequest.create(ctx.request.body);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postTransactionRequests is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'TransactionRequests is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -169,7 +168,7 @@ const postTransactionRequests = async (ctx) => {
const postBulkTransfers = async (ctx) => {
try {
const res = await ctx.state.model.bulkTransfer.create(ctx.request.body);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postBulkTransfers is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'BulkTransfers is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -188,7 +187,7 @@ const getBulkTransferById = async (ctx) => {
ctx.response.status = 404;
return;
}
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getBulkTransferById is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'BulkTransferById is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand All @@ -207,7 +206,7 @@ const getAccountsByUserId = async (ctx) => {
try {
const { ID } = ctx.state.path.params;
// if rules not configured, return ID not found error
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getAccountsByUserId rules not configured for : ${ID}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'AccountsByUserId rules not configured for ID', ID});
ctx.response.body = ApiErrorCodes.ID_NOT_FOUND;
ctx.response.status = 404;
return;
Expand Down Expand Up @@ -238,7 +237,7 @@ const getScopesById = async (ctx) => {
},
],
};
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getScopesById is returning body: ${res}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'ScopesById is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};
Expand All @@ -249,14 +248,14 @@ const postValidateAuthToken = async (ctx) => {
const res = {
isValid: ctx.request.body.authToken % 2 === 0,
};
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`postValidateOTP is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'ValidateOTP is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};

const validateConsentRequests = async (ctx) => {
const request = ctx.request.body;
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`validateConsentRequests request body: ${util.inspect(request)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'validateConsentRequests request body', body: request});
// default mock response, if rules not configured
const res = {
isValid: true,
Expand All @@ -265,44 +264,44 @@ const validateConsentRequests = async (ctx) => {
authUri: `dfspa.com/authorize?consentRequestId=${request.id}`,
},
};
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`validateConsentRequests is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'validateConsentRequests is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};

const sendOTP = async (ctx) => {
const request = ctx.request.body;
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`sendOTP request body: ${util.inspect(request)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'sendOTP request body', body: request});
// default mock reponse, if rules not configured
const res = {
otp: Math.floor(Math.random() * 90000) + 10000,
};
await objectStore.set(`${request.consentRequestId}-OTP`, res);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`sendOTP is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'sendOTP is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};

const storeConsentRequest = async (ctx) => {
const { ID } = ctx.state.path.params;
const request = ctx.request.body;
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`storeConsentRequest request body: ${util.inspect(request)}`);
// default mock reponse, if rules not configured
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'storeConsentRequest request body', body: request});
// default mock response, if rules not configured
const res = {
status: 'OK',
};
await objectStore.set(`${ID}-CR`, request);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`sendOTP is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({msg: 'sendOTP is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
};

const getConsentRequest = async (ctx) => {
const { ID } = ctx.state.path.params;
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getConsentRequest : ${ID}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: `ConsentRequest : ${ID}`});
// default mock response, if rules not configured
const res = await objectStore.get(`${ID}-CR`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getConsentRequest : ${ID} is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: `ConsentRequest : ${ID} is returning body`, body: res});
ctx.response.body = res;
ctx.response.status = 200;
};
Expand All @@ -313,7 +312,7 @@ const getSignedChallenge = async (ctx) => {
pinValue: crypto.randomBytes(256).toString('base64').slice(0, 64),
counter: '1',
};
ctx.state.logger.isInfoEnabled && ctx.state.logger.info(`getSignedChallenge is returning body: ${util.inspect(res)}`);
ctx.state.logger.isInfoEnabled && ctx.state.logger.info({ msg: 'SignedChallenge is returning body', body: res});
ctx.response.body = res;
ctx.response.status = 200;
} catch (err) {
Expand Down
Loading

0 comments on commit 6583dec

Please sign in to comment.