Skip to content

Commit

Permalink
feat(mojaloop/#3759)!: change logger functions to object variables (#196
Browse files Browse the repository at this point in the history
)

* feat!: change logger functions to object variables

* dep update
  • Loading branch information
kleyow authored Feb 29, 2024
1 parent ad3b480 commit f78818d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 93 deletions.
94 changes: 47 additions & 47 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
"devDependencies": {
"@mojaloop/api-snippets": "^17.4.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.19",
"@types/node": "^20.11.21",
"audit-ci": "^6.6.1",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.29.1",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"nock": "^13.5.3",
"nock": "^13.5.4",
"npm-check-updates": "^16.14.15",
"pre-commit": "^1.2.2",
"replace": "^1.2.2",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/WSO2Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class WSO2Auth extends EventEmitter {
// Prevent the timeout from expiring and triggering an extraneous refresh
this.stop();

this._logger.isDebugEnabled() && this._logger.debug('WSO2 token refresh initiated');
this._logger.isDebugEnabled && this._logger.debug('WSO2 token refresh initiated');
const reqOpts = {
...this._reqOpts,
headers: {
Expand All @@ -112,7 +112,7 @@ class WSO2Auth extends EventEmitter {
let refreshSeconds;
try {
const response = await request(reqOpts);
this._logger.isDebugEnabled() && this._logger.push({ reqOpts: { ...reqOpts, agent: '[REDACTED]' }, response }).debug('Response received from WSO2');
this._logger.isDebugEnabled && this._logger.push({ reqOpts: { ...reqOpts, agent: '[REDACTED]' }, response }).debug('Response received from WSO2');
if (response.statusCode > 299) {
this.emit('error', 'Error retrieving WSO2 auth token');
throw new Error(`Unexpected response code ${response.statusCode} received from WSO2 token request`);
Expand All @@ -122,11 +122,11 @@ class WSO2Auth extends EventEmitter {
const tokenIsValidNumber = (typeof expires_in === 'number') && (expires_in > 0);
const tokenExpiry = tokenIsValidNumber ? expires_in : Infinity;
refreshSeconds = Math.min(this._refreshSeconds, tokenExpiry);
this._logger.isDebugEnabled() && this._logger.debug('WSO2 token refreshed successfully. ' +
this._logger.isDebugEnabled && this._logger.debug('WSO2 token refreshed successfully. ' +
`Token expiry is ${expires_in}${tokenIsValidNumber ? 's' : ''}, ` +
`next refresh in ${refreshSeconds}s`);
} catch (error) {
this._logger.isDebugEnabled() && this._logger.debug(`Error performing WSO2 token refresh: ${error.message}. `
this._logger.isDebugEnabled && this._logger.debug(`Error performing WSO2 token refresh: ${error.message}. `
+ `Retry in ${this._refreshRetrySeconds}s`);
refreshSeconds = this._refreshRetrySeconds;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ilp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Ilp {
condition: generatedCondition
};

this.logger.isDebugEnabled() && this.logger.debug(`Generated ILP: transaction object: ${safeStringify(transactionObject)}\nPacket input: ${safeStringify(packetInput)}\nOutput: ${safeStringify(ret)}`);
this.logger.isDebugEnabled && this.logger.debug(`Generated ILP: transaction object: ${safeStringify(transactionObject)}\nPacket input: ${safeStringify(packetInput)}\nOutput: ${safeStringify(ret)}`);

return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/jws/jwsSigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class JwsSigner {
* (see https://github.com/axios/axios)
*/
sign(requestOptions) {
this.logger.isDebugEnabled() && this.logger.debug(`JWS Signing request: ${safeStringify(requestOptions)}`);
this.logger.isDebugEnabled && this.logger.debug(`JWS Signing request: ${safeStringify(requestOptions)}`);
const payload = requestOptions.body || requestOptions.data;
const uri = requestOptions.uri || requestOptions.url;

Expand Down Expand Up @@ -81,7 +81,7 @@ class JwsSigner {
* @returns {string} - JWS Signature as a string
*/
getSignature(requestOptions) {
this.logger.isDebugEnabled() && this.logger.debug(`Get JWS Signature: ${safeStringify(requestOptions)}`);
this.logger.isDebugEnabled && this.logger.debug(`Get JWS Signature: ${safeStringify(requestOptions)}`);
const payload = requestOptions.body || requestOptions.data;
const uri = requestOptions.uri || requestOptions.url;

Expand Down
8 changes: 4 additions & 4 deletions src/lib/jws/jwsValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JwsValidator {
const { headers, body, data } = request;
const payload = body || data;

this.logger.isDebugEnabled() && this.logger.debug(`Validating JWS on request with headers: ${safeStringify(headers)} and body: ${safeStringify(payload)}`);
this.logger.isDebugEnabled && this.logger.debug(`Validating JWS on request with headers: ${safeStringify(headers)} and body: ${safeStringify(payload)}`);

if(!payload) {
throw new Error('Cannot validate JWS without a body');
Expand Down Expand Up @@ -80,13 +80,13 @@ class JwsValidator {
this._validateProtectedHeader(headers, result.header);

// const result = jwt.verify(token, pubKey, { complete: true, json: true });
this.logger.isDebugEnabled() && this.logger.debug(`JWS verify result: ${safeStringify(result)}`);
this.logger.isDebugEnabled && this.logger.debug(`JWS verify result: ${safeStringify(result)}`);

// all ok if we got here
this.logger.isDebugEnabled() && this.logger.debug(`JWS valid for request ${safeStringify(request)}`);
this.logger.isDebugEnabled && this.logger.debug(`JWS valid for request ${safeStringify(request)}`);
}
catch(err) {
this.logger.isDebugEnabled() && this.logger.debug(`Error validating JWS: ${err.stack || safeStringify(err)}`);
this.logger.isDebugEnabled && this.logger.debug(`Error validating JWS: ${err.stack || safeStringify(err)}`);
throw err;
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/lib/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
// 2) No 'replace' method (or method for overwriting logged context) has been implemented. This is
// for the same reason no 'pop' method has been implemented.

// It is extremely important that this logger define the similar function/object names to
// that of @mojaloop/central-services-logger.
// Various core services use the JWS signer with the central services logger
// while the mojaloop-sdk uses the logger defined here
const util = require('util');
const safeStringify = require('fast-safe-stringify');

Expand Down Expand Up @@ -107,6 +111,13 @@ class Logger {
} = {},
} = {}) {
this[contextSym] = context;
this.isVerboseEnabled = false;
this.isDebugEnabled = false;
this.isWarnEnabled = false;
this.isErrorEnabled = false;
this.isTraceEnabled = false;
this.isInfoEnabled = false;
this.isFatalEnabled = false;
this.configure({
stringify,
opts: {
Expand All @@ -131,9 +142,7 @@ class Logger {
this[level] = (...args) => {
this._log(level, ...args);
};
this[`is${level[0].toUpperCase()}${level.slice(1).toLowerCase()}Enabled`] = () => {
return true;
};
this[`is${level[0].toUpperCase()}${level.slice(1).toLowerCase()}Enabled`] = true;
});
}

Expand Down Expand Up @@ -194,19 +203,12 @@ class Logger {
// so that the logger does not throw an error.
// An easy way to do level silencing.
verbose(){}
isVerboseEnabled(){ return false; }
debug(){}
isDebugEnabled(){ return false; }
warn(){}
isWarnEnabled(){ return false; }
error(){}
isErrorEnabled(){ return false; }
trace(){}
isTraceEnabled(){ return false; }
info(){}
isInfoEnabled(){ return false; }
fatal(){}
isFatalEnabled(){ return false; }
}

module.exports = {
Expand Down
Loading

0 comments on commit f78818d

Please sign in to comment.