'use strict';
-const path = require('path');
-
-/**
- * Return a function which combines a part of the filename with a scope, for use in logging.
- * @param {string} filename
- */
-const fileScope = (filename) => {
- let fScope = path.basename(filename, '.js');
- if (fScope === 'index') {
- fScope = path.basename(path.dirname(filename));
- }
- return (scope) => `${fScope}:${scope}`;
-}
-
-
/**
- * Pick out useful axios response fields.
- * @param {*} res
- * @returns
+ * Pick out useful got response fields.
+ * @param {GotResponse} res
+ * @returns {Object}
*/
-const axiosResponseLogData = (res) => {
+const gotResponseLogData = (res) => {
const data = pick(res, [
- 'status',
- 'statusText',
+ 'statusCode',
+ 'statusMessage',
'headers',
- 'elapsedTimeMs',
- 'data',
+ 'body',
+ 'error',
]);
- if (data.data) {
- data.data = logTruncate(data.data, 100);
+ if (typeof res.body === 'string') {
+ data.body = logTruncate(data.body, 100);
+ } else if (res.body instanceof Buffer) {
+ data.body = `<Buffer ${res.body.byteLength} bytes>`;
+ }
+ data.elapsedTimeMs = res?.timings?.phases?.total;
+ if (res?.redirectUrls?.length) {
+ data.redirectUrls = res.redirectUrls;
+ }
+ if (res?.retryCount) {
+ data.retryCount = res.retryCount;
}
return data;
};
module.exports = {
- fileScope,
- axiosResponseLogData,
+ gotResponseLogData,
logTruncate,
pick,
setSymmetricDifference,
properURLComponentName,
-};
\ No newline at end of file
+};