Skip to content

Commit

Permalink
EPMRPP-89147 || Add debug logs for RestClient and execution time (#188)
Browse files Browse the repository at this point in the history
* EPMRPP-89147 || Add debug logs for RestClient and execution time

* refactor: remove execution time from report-portal-client logs

* fix: check if restClientConfig exists
  • Loading branch information
AliakseiLiasnitski authored Jan 23, 2024
1 parent 0328524 commit 5ab0747
Showing 1 changed file with 43 additions and 0 deletions.
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;

0 comments on commit 5ab0747

Please sign in to comment.