diff --git a/src/constants.ts b/src/constants.ts index 5dc4332..d669c43 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -6,6 +6,10 @@ const defaultConfig = { remoteConfigFetchEndpoint: '/config', telemetryEndpoint: '/telemetry', allowLocalUrls: false, + logRequestHeaders: true, + logRequestBody: true, + logResponseHeaders: true, + logResponseBody: true, ignoredDomains: [], // After the close command is sent, wait for this many milliseconds before diff --git a/src/index.ts b/src/index.ts index e4e1e8b..6c52aaa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -144,12 +144,12 @@ const Supergood = () => { const body = await request.clone().text(); const requestData = { id: requestId, - headers: Object.fromEntries(request.headers.entries()), + headers: supergoodConfig.logRequestHeaders ? Object.fromEntries(request.headers.entries()) : {}, method: request.method, url: url.href, path: url.pathname, search: url.search, - body: safeParseJson(body), + body: supergoodConfig.logRequestBody ? safeParseJson(body) : {}, requestedAt: new Date() } as RequestType; @@ -197,10 +197,10 @@ const Supergood = () => { const responseData = { response: { - headers: Object.fromEntries(response.headers.entries()), + headers: supergoodConfig.logResponseHeaders ? Object.fromEntries(response.headers.entries()) : {}, status: response.status, statusText: response.statusText, - body: response.body && safeParseJson(response.body), + body: supergoodConfig.logResponseBody ? response.body && safeParseJson(response.body) : {}, respondedAt: new Date() }, ...requestData diff --git a/src/types.ts b/src/types.ts index 7ae7dce..776c587 100644 --- a/src/types.ts +++ b/src/types.ts @@ -47,6 +47,10 @@ interface ConfigType { telemetryEndpoint: string; // Defaults to {baseUrl}/telemetry if not provided waitAfterClose: number; remoteConfig: RemoteConfigType; + logRequestHeaders: boolean; + logRequestBody: boolean; + logResponseHeaders: boolean; + logResponseBody: boolean; } interface TelemetryType { diff --git a/src/utils.ts b/src/utils.ts index e9f3eb3..991656e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -35,12 +35,14 @@ const logger = ({ error: Error, { reportOut }: { reportOut: boolean } = { reportOut: true } ) => { - console.error( - new Date().toISOString(), - `${packageName}@${packageVersion}: ${message}`, - JSON.stringify(payload, null, 2), - error - ); + if (process.env.SUPERGOOD_LOG_LEVEL === 'debug') { + console.error( + new Date().toISOString(), + `${packageName}@${packageVersion}: ${message}`, + JSON.stringify(payload, null, 2), + error + ); + } if (reportOut && errorSinkUrl) { postError( errorSinkUrl, diff --git a/test/e2e/core.e2e.test.ts b/test/e2e/core.e2e.test.ts index e1405e4..57fa937 100644 --- a/test/e2e/core.e2e.test.ts +++ b/test/e2e/core.e2e.test.ts @@ -262,6 +262,80 @@ describe('core functionality', () => { }); }); + describe('log bodies', () => { + it('should not log the requestHeaders if specified in config', async () => { + await Supergood.init( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logRequestHeaders: false }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET, + }, + SUPERGOOD_SERVER + ); + await axios.get(`${MOCK_DATA_SERVER}/posts`); + await Supergood.close(); + checkPostedEvents(postEventsMock, 1, { + request: expect.objectContaining({ + headers: {} + }) + }); + }); + + it('should not log the requestBody if specified in config', async () => { + await Supergood.init( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logRequestBody: false }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET, + }, + SUPERGOOD_SERVER + ); + await axios.get(`${MOCK_DATA_SERVER}/posts`); + await Supergood.close(); + checkPostedEvents(postEventsMock, 1, { + request: expect.objectContaining({ + body: {} + }) + }); + }); + + it('should not log the responseHeaders if specified in config', async () => { + await Supergood.init( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logResponseHeaders: false }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET, + }, + SUPERGOOD_SERVER + ); + await axios.get(`${MOCK_DATA_SERVER}/posts`); + await Supergood.close(); + checkPostedEvents(postEventsMock, 1, { + response: expect.objectContaining({ + headers: {} + }) + }); + }); + + it('should not log the responseBody if specified in config', async () => { + await Supergood.init( + { + config: { ...SUPERGOOD_CONFIG, allowLocalUrls: true, logResponseBody: false }, + clientId: SUPERGOOD_CLIENT_ID, + clientSecret: SUPERGOOD_CLIENT_SECRET, + }, + SUPERGOOD_SERVER + ); + await axios.get(`${MOCK_DATA_SERVER}/posts`); + await Supergood.close(); + checkPostedEvents(postEventsMock, 1, { + response: expect.objectContaining({ + body: {} + }) + }); + }); + }) + describe('headers', () => { it('should capture custom request headers', async () => { await Supergood.init(