Skip to content

Commit c70fa71

Browse files
AbhiPrasadmsonnb
authored andcommitted
ref(replay-internal): Use debug instead of logger (#16987)
resolves #16937 Also reaches into `packages/browser-utils/src/networkUtils.ts` to make a quick change. Please review `packages/replay-internal/src/util/logger.ts` carefully.
1 parent 966c1dd commit c70fa71

File tree

19 files changed

+112
-115
lines changed

19 files changed

+112
-115
lines changed

dev-packages/browser-integration-tests/suites/replay/logger/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ sentryTest('should output logger messages', async ({ getLocalTestUrl, page }) =>
2121
await Promise.all([page.goto(url), reqPromise0]);
2222

2323
expect(messages).toContain('Sentry Logger [log]: Integration installed: Replay');
24-
expect(messages).toContain('Sentry Logger [info]: [Replay] Creating new session');
25-
expect(messages).toContain('Sentry Logger [info]: [Replay] Starting replay in session mode');
26-
expect(messages).toContain('Sentry Logger [info]: [Replay] Using compression worker');
24+
expect(messages).toContain('Sentry Logger [log]: [Replay] Creating new session');
25+
expect(messages).toContain('Sentry Logger [log]: [Replay] Starting replay in session mode');
26+
expect(messages).toContain('Sentry Logger [log]: [Replay] Using compression worker');
2727
});

packages/browser-utils/src/networkUtils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Logger } from '@sentry/core';
2-
import { logger } from '@sentry/core';
1+
import { debug } from '@sentry/core';
32
import { DEBUG_BUILD } from './debug-build';
43
import type { NetworkMetaWarning } from './types';
54

@@ -16,7 +15,7 @@ export function serializeFormData(formData: FormData): string {
1615
}
1716

1817
/** Get the string representation of a body. */
19-
export function getBodyString(body: unknown, _logger: Logger = logger): [string | undefined, NetworkMetaWarning?] {
18+
export function getBodyString(body: unknown, _debug: typeof debug = debug): [string | undefined, NetworkMetaWarning?] {
2019
try {
2120
if (typeof body === 'string') {
2221
return [body];
@@ -34,11 +33,11 @@ export function getBodyString(body: unknown, _logger: Logger = logger): [string
3433
return [undefined];
3534
}
3635
} catch (error) {
37-
DEBUG_BUILD && _logger.error(error, 'Failed to serialize body', body);
36+
DEBUG_BUILD && _debug.error(error, 'Failed to serialize body', body);
3837
return [undefined, 'BODY_PARSE_ERROR'];
3938
}
4039

41-
DEBUG_BUILD && _logger.info('Skipping network body because of body type', body);
40+
DEBUG_BUILD && _debug.log('Skipping network body because of body type', body);
4241

4342
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
4443
}

packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DEBUG_BUILD } from '../debug-build';
33
import type { ReplayContainer } from '../types';
44
import { isErrorEvent, isFeedbackEvent, isReplayEvent, isTransactionEvent } from '../util/eventUtils';
55
import { isRrwebError } from '../util/isRrwebError';
6-
import { logger } from '../util/logger';
6+
import { debug } from '../util/logger';
77
import { resetReplayIdOnDynamicSamplingContext } from '../util/resetReplayIdOnDynamicSamplingContext';
88
import { addFeedbackBreadcrumb } from './util/addFeedbackBreadcrumb';
99
import { shouldSampleForBufferEvent } from './util/shouldSampleForBufferEvent';
@@ -52,7 +52,7 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even
5252
// Unless `captureExceptions` is enabled, we want to ignore errors coming from rrweb
5353
// As there can be a bunch of stuff going wrong in internals there, that we don't want to bubble up to users
5454
if (isRrwebError(event, hint) && !replay.getOptions()._experiments.captureExceptions) {
55-
DEBUG_BUILD && logger.log('Ignoring error from rrweb internals', event);
55+
DEBUG_BUILD && debug.log('Ignoring error from rrweb internals', event);
5656
return null;
5757
}
5858

packages/replay-internal/src/coreHandlers/handleNetworkBreadcrumbs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getClient } from '@sentry/core';
33
import type { FetchHint, XhrHint } from '@sentry-internal/browser-utils';
44
import { DEBUG_BUILD } from '../debug-build';
55
import type { ReplayContainer, ReplayNetworkOptions } from '../types';
6-
import { logger } from '../util/logger';
6+
import { debug } from '../util/logger';
77
import { captureFetchBreadcrumbToReplay, enrichFetchBreadcrumb } from './util/fetchUtils';
88
import { captureXhrBreadcrumbToReplay, enrichXhrBreadcrumb } from './util/xhrUtils';
99

@@ -79,7 +79,7 @@ export function beforeAddNetworkBreadcrumb(
7979
captureFetchBreadcrumbToReplay(breadcrumb, hint, options);
8080
}
8181
} catch (e) {
82-
DEBUG_BUILD && logger.exception(e, 'Error when enriching network breadcrumb');
82+
DEBUG_BUILD && debug.exception(e, 'Error when enriching network breadcrumb');
8383
}
8484
}
8585

packages/replay-internal/src/coreHandlers/util/fetchUtils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
ReplayNetworkRequestData,
99
ReplayNetworkRequestOrResponse,
1010
} from '../../types';
11-
import { logger } from '../../util/logger';
11+
import { debug } from '../../util/logger';
1212
import { addNetworkBreadcrumb } from './addNetworkBreadcrumb';
1313
import {
1414
buildNetworkRequestOrResponse,
@@ -39,7 +39,7 @@ export async function captureFetchBreadcrumbToReplay(
3939
const result = makeNetworkReplayBreadcrumb('resource.fetch', data);
4040
addNetworkBreadcrumb(options.replay, result);
4141
} catch (error) {
42-
DEBUG_BUILD && logger.exception(error, 'Failed to capture fetch breadcrumb');
42+
DEBUG_BUILD && debug.exception(error, 'Failed to capture fetch breadcrumb');
4343
}
4444
}
4545

@@ -115,7 +115,7 @@ function _getRequestInfo(
115115

116116
// We only want to transmit string or string-like bodies
117117
const requestBody = getFetchRequestArgBody(input);
118-
const [bodyStr, warning] = getBodyString(requestBody, logger);
118+
const [bodyStr, warning] = getBodyString(requestBody, debug);
119119
const data = buildNetworkRequestOrResponse(headers, requestBodySize, bodyStr);
120120

121121
if (warning) {
@@ -188,7 +188,7 @@ function getResponseData(
188188

189189
return buildNetworkRequestOrResponse(headers, size, undefined);
190190
} catch (error) {
191-
DEBUG_BUILD && logger.exception(error, 'Failed to serialize response body');
191+
DEBUG_BUILD && debug.exception(error, 'Failed to serialize response body');
192192
// fallback
193193
return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
194194
}
@@ -206,11 +206,11 @@ async function _parseFetchResponseBody(response: Response): Promise<[string | un
206206
return [text];
207207
} catch (error) {
208208
if (error instanceof Error && error.message.indexOf('Timeout') > -1) {
209-
DEBUG_BUILD && logger.warn('Parsing text body from response timed out');
209+
DEBUG_BUILD && debug.warn('Parsing text body from response timed out');
210210
return [undefined, 'BODY_PARSE_TIMEOUT'];
211211
}
212212

213-
DEBUG_BUILD && logger.exception(error, 'Failed to get text body from response');
213+
DEBUG_BUILD && debug.exception(error, 'Failed to get text body from response');
214214
return [undefined, 'BODY_PARSE_ERROR'];
215215
}
216216
}
@@ -271,7 +271,7 @@ function _tryCloneResponse(response: Response): Response | void {
271271
return response.clone();
272272
} catch (error) {
273273
// this can throw if the response was already consumed before
274-
DEBUG_BUILD && logger.exception(error, 'Failed to clone response body');
274+
DEBUG_BUILD && debug.exception(error, 'Failed to clone response body');
275275
}
276276
}
277277

packages/replay-internal/src/coreHandlers/util/xhrUtils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { NetworkMetaWarning, XhrHint } from '@sentry-internal/browser-utils
33
import { getBodyString, SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils';
44
import { DEBUG_BUILD } from '../../debug-build';
55
import type { ReplayContainer, ReplayNetworkOptions, ReplayNetworkRequestData } from '../../types';
6-
import { logger } from '../../util/logger';
6+
import { debug } from '../../util/logger';
77
import { addNetworkBreadcrumb } from './addNetworkBreadcrumb';
88
import {
99
buildNetworkRequestOrResponse,
@@ -32,7 +32,7 @@ export async function captureXhrBreadcrumbToReplay(
3232
const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
3333
addNetworkBreadcrumb(options.replay, result);
3434
} catch (error) {
35-
DEBUG_BUILD && logger.exception(error, 'Failed to capture xhr breadcrumb');
35+
DEBUG_BUILD && debug.exception(error, 'Failed to capture xhr breadcrumb');
3636
}
3737
}
3838

@@ -106,7 +106,7 @@ function _prepareXhrData(
106106
: {};
107107
const networkResponseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.networkResponseHeaders);
108108

109-
const [requestBody, requestWarning] = options.networkCaptureBodies ? getBodyString(input, logger) : [undefined];
109+
const [requestBody, requestWarning] = options.networkCaptureBodies ? getBodyString(input, debug) : [undefined];
110110
const [responseBody, responseWarning] = options.networkCaptureBodies ? _getXhrResponseBody(xhr) : [undefined];
111111

112112
const request = buildNetworkRequestOrResponse(networkRequestHeaders, requestBodySize, requestBody);
@@ -156,7 +156,7 @@ function _getXhrResponseBody(xhr: XMLHttpRequest): [string | undefined, NetworkM
156156
errors.push(e);
157157
}
158158

159-
DEBUG_BUILD && logger.warn('Failed to get xhr response body', ...errors);
159+
DEBUG_BUILD && debug.warn('Failed to get xhr response body', ...errors);
160160

161161
return [undefined];
162162
}
@@ -193,11 +193,11 @@ export function _parseXhrResponse(
193193
return [undefined];
194194
}
195195
} catch (error) {
196-
DEBUG_BUILD && logger.exception(error, 'Failed to serialize body', body);
196+
DEBUG_BUILD && debug.exception(error, 'Failed to serialize body', body);
197197
return [undefined, 'BODY_PARSE_ERROR'];
198198
}
199199

200-
DEBUG_BUILD && logger.info('Skipping network body because of body type', body);
200+
DEBUG_BUILD && debug.log('Skipping network body because of body type', body);
201201

202202
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
203203
}

packages/replay-internal/src/eventBuffer/EventBufferCompressionWorker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ReplayRecordingData } from '@sentry/core';
22
import { REPLAY_MAX_EVENT_BUFFER_SIZE } from '../constants';
33
import { DEBUG_BUILD } from '../debug-build';
44
import type { AddEventResult, EventBuffer, EventBufferType, RecordingEvent } from '../types';
5-
import { logger } from '../util/logger';
5+
import { debug } from '../util/logger';
66
import { timestampToMs } from '../util/timestamp';
77
import { EventBufferSizeExceededError } from './error';
88
import { WorkerHandler } from './WorkerHandler';
@@ -91,7 +91,7 @@ export class EventBufferCompressionWorker implements EventBuffer {
9191

9292
// We do not wait on this, as we assume the order of messages is consistent for the worker
9393
this._worker.postMessage('clear').then(null, e => {
94-
DEBUG_BUILD && logger.exception(e, 'Sending "clear" message to worker failed', e);
94+
DEBUG_BUILD && debug.exception(e, 'Sending "clear" message to worker failed', e);
9595
});
9696
}
9797

packages/replay-internal/src/eventBuffer/EventBufferProxy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReplayRecordingData } from '@sentry/core';
22
import { DEBUG_BUILD } from '../debug-build';
33
import type { AddEventResult, EventBuffer, EventBufferType, RecordingEvent } from '../types';
4-
import { logger } from '../util/logger';
4+
import { debug } from '../util/logger';
55
import { EventBufferArray } from './EventBufferArray';
66
import { EventBufferCompressionWorker } from './EventBufferCompressionWorker';
77

@@ -99,7 +99,7 @@ export class EventBufferProxy implements EventBuffer {
9999
} catch (error) {
100100
// If the worker fails to load, we fall back to the simple buffer.
101101
// Nothing more to do from our side here
102-
DEBUG_BUILD && logger.exception(error, 'Failed to load the compression worker, falling back to simple buffer');
102+
DEBUG_BUILD && debug.exception(error, 'Failed to load the compression worker, falling back to simple buffer');
103103
return;
104104
}
105105

@@ -130,7 +130,7 @@ export class EventBufferProxy implements EventBuffer {
130130
// Can now clear fallback buffer as it's no longer necessary
131131
this._fallback.clear();
132132
} catch (error) {
133-
DEBUG_BUILD && logger.exception(error, 'Failed to add events when switching buffers.');
133+
DEBUG_BUILD && debug.exception(error, 'Failed to add events when switching buffers.');
134134
}
135135
}
136136
}

packages/replay-internal/src/eventBuffer/WorkerHandler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEBUG_BUILD } from '../debug-build';
22
import type { WorkerRequest, WorkerResponse } from '../types';
3-
import { logger } from '../util/logger';
3+
import { debug } from '../util/logger';
44

55
/**
66
* Event buffer that uses a web worker to compress events.
@@ -55,7 +55,7 @@ export class WorkerHandler {
5555
* Destroy the worker.
5656
*/
5757
public destroy(): void {
58-
DEBUG_BUILD && logger.info('Destroying compression worker');
58+
DEBUG_BUILD && debug.log('Destroying compression worker');
5959
this._worker.terminate();
6060
}
6161

@@ -83,7 +83,7 @@ export class WorkerHandler {
8383

8484
if (!response.success) {
8585
// TODO: Do some error handling, not sure what
86-
DEBUG_BUILD && logger.error('Error in compression worker: ', response.response);
86+
DEBUG_BUILD && debug.error('Error in compression worker: ', response.response);
8787

8888
reject(new Error('Error in compression worker'));
8989
return;

packages/replay-internal/src/eventBuffer/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getWorkerURL } from '@sentry-internal/replay-worker';
22
import { DEBUG_BUILD } from '../debug-build';
33
import type { EventBuffer, ReplayWorkerURL } from '../types';
4-
import { logger } from '../util/logger';
4+
import { debug } from '../util/logger';
55
import { EventBufferArray } from './EventBufferArray';
66
import { EventBufferProxy } from './EventBufferProxy';
77

@@ -32,7 +32,7 @@ export function createEventBuffer({
3232
}
3333
}
3434

35-
DEBUG_BUILD && logger.info('Using simple buffer');
35+
DEBUG_BUILD && debug.log('Using simple buffer');
3636
return new EventBufferArray();
3737
}
3838

@@ -44,11 +44,11 @@ function _loadWorker(customWorkerUrl?: ReplayWorkerURL): EventBufferProxy | void
4444
return;
4545
}
4646

47-
DEBUG_BUILD && logger.info(`Using compression worker${customWorkerUrl ? ` from ${customWorkerUrl}` : ''}`);
47+
DEBUG_BUILD && debug.log(`Using compression worker${customWorkerUrl ? ` from ${customWorkerUrl}` : ''}`);
4848
const worker = new Worker(workerUrl);
4949
return new EventBufferProxy(worker);
5050
} catch (error) {
51-
DEBUG_BUILD && logger.exception(error, 'Failed to create compression worker');
51+
DEBUG_BUILD && debug.exception(error, 'Failed to create compression worker');
5252
// Fall back to use simple event buffer array
5353
}
5454
}

0 commit comments

Comments
 (0)