Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPMRPP-89147 || Add debug logs for RestClient and execution time #188

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RestClient {
this.baseURL = options.baseURL;
this.headers = options.headers;
this.restClientConfig = options.restClientConfig;

addLogger(this.restClientConfig ? this.restClientConfig.debug : false);
}

buildPath(path) {
Expand Down Expand Up @@ -112,4 +114,45 @@ method: ${method}`,
}
}

const addLogger = (debug) => {
if (debug) {
axios.interceptors.request.use((config) => {
const startDate = new Date();
config.startTime = startDate.valueOf();

console.log(`Request method=${config.method} url=${config.url} [${startDate.toISOString()}]`);

return config;
});

axios.interceptors.response.use(
(response) => {
const date = new Date();
const { status, config } = response;

console.log(
`Response status=${status} url=${config.url} time=${
date.valueOf() - config.startTime
}ms [${date.toISOString()}]`,
);

return response;
},
(error) => {
const date = new Date();
const { response, config } = error;
const status = response ? response.status : null;

console.log(
`Response ${status ? 'status=' + status : "message='" + error.message + "'"} url=${
config.url
} time=${date.valueOf() - config.startTime}ms [${date.toISOString()}]`,
);

return Promise.reject(error);
},
);
}
};

module.exports = RestClient;
Loading