Skip to content

Commit abc7259

Browse files
authored
feat(replay): Add a replay-specific logger (#13256)
Removes the old `logInfo` function and replaces it with a new replay-specific logger. Configuration is done when the replay integration is first initialized to avoid needing to pass around configuration options. This also means that we cannot select individual log statements to be added as breadcrumbs with `traceInternals` options. This also adds a `logger.exception` that wraps `captureException`. Note that only the following logging levels are supported: * `info` * `log` * `warn` * `error` With two additions: * `exception` * `infoTick` (needs a better name) - same as `info` but adds the breadcrumb in the next tick due to some pre-existing race conditions There is one method to configure the logger: * `setConfig({ traceInternals, captureExceptions })`
1 parent 5aef4a0 commit abc7259

18 files changed

+198
-163
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Event, EventHint } from '@sentry/types';
2-
import { logger } from '@sentry/utils';
32

43
import { DEBUG_BUILD } from '../debug-build';
54
import type { ReplayContainer } from '../types';
65
import { isErrorEvent, isFeedbackEvent, isReplayEvent, isTransactionEvent } from '../util/eventUtils';
76
import { isRrwebError } from '../util/isRrwebError';
7+
import { logger } from '../util/logger';
88
import { addFeedbackBreadcrumb } from './util/addFeedbackBreadcrumb';
99
import { shouldSampleForBufferEvent } from './util/shouldSampleForBufferEvent';
1010

@@ -50,7 +50,7 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even
5050
// Unless `captureExceptions` is enabled, we want to ignore errors coming from rrweb
5151
// As there can be a bunch of stuff going wrong in internals there, that we don't want to bubble up to users
5252
if (isRrwebError(event, hint) && !replay.getOptions()._experiments.captureExceptions) {
53-
DEBUG_BUILD && logger.log('[Replay] Ignoring error from rrweb internals', event);
53+
DEBUG_BUILD && logger.log('Ignoring error from rrweb internals', event);
5454
return null;
5555
}
5656

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { getClient } from '@sentry/core';
22
import type { Breadcrumb, BreadcrumbHint, FetchBreadcrumbData, XhrBreadcrumbData } from '@sentry/types';
3-
import { logger } from '@sentry/utils';
43

54
import { DEBUG_BUILD } from '../debug-build';
65
import type { FetchHint, ReplayContainer, ReplayNetworkOptions, XhrHint } from '../types';
6+
import { logger } 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.warn('Error when enriching network breadcrumb');
82+
DEBUG_BUILD && logger.exception(e, 'Error when enriching network breadcrumb');
8383
}
8484
}
8585

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { setTimeout } from '@sentry-internal/browser-utils';
22
import type { Breadcrumb, FetchBreadcrumbData } from '@sentry/types';
3-
import { logger } from '@sentry/utils';
43

54
import { DEBUG_BUILD } from '../../debug-build';
65
import type {
@@ -11,6 +10,7 @@ import type {
1110
ReplayNetworkRequestData,
1211
ReplayNetworkRequestOrResponse,
1312
} from '../../types';
13+
import { logger } from '../../util/logger';
1414
import { addNetworkBreadcrumb } from './addNetworkBreadcrumb';
1515
import {
1616
buildNetworkRequestOrResponse,
@@ -42,7 +42,7 @@ export async function captureFetchBreadcrumbToReplay(
4242
const result = makeNetworkReplayBreadcrumb('resource.fetch', data);
4343
addNetworkBreadcrumb(options.replay, result);
4444
} catch (error) {
45-
DEBUG_BUILD && logger.error('[Replay] Failed to capture fetch breadcrumb', error);
45+
DEBUG_BUILD && logger.exception(error, 'Failed to capture fetch breadcrumb');
4646
}
4747
}
4848

@@ -192,7 +192,7 @@ function getResponseData(
192192

193193
return buildNetworkRequestOrResponse(headers, size, undefined);
194194
} catch (error) {
195-
DEBUG_BUILD && logger.warn('[Replay] Failed to serialize response body', error);
195+
DEBUG_BUILD && logger.exception(error, 'Failed to serialize response body');
196196
// fallback
197197
return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
198198
}
@@ -209,7 +209,7 @@ async function _parseFetchResponseBody(response: Response): Promise<[string | un
209209
const text = await _tryGetResponseText(res);
210210
return [text];
211211
} catch (error) {
212-
DEBUG_BUILD && logger.warn('[Replay] Failed to get text body from response', error);
212+
DEBUG_BUILD && logger.exception(error, 'Failed to get text body from response');
213213
return [undefined, 'BODY_PARSE_ERROR'];
214214
}
215215
}
@@ -279,7 +279,7 @@ function _tryCloneResponse(response: Response): Response | void {
279279
return response.clone();
280280
} catch (error) {
281281
// this can throw if the response was already consumed before
282-
DEBUG_BUILD && logger.warn('[Replay] Failed to clone response body', error);
282+
DEBUG_BUILD && logger.exception(error, 'Failed to clone response body');
283283
}
284284
}
285285

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dropUndefinedKeys, logger, stringMatchesSomePattern } from '@sentry/utils';
1+
import { dropUndefinedKeys, stringMatchesSomePattern } from '@sentry/utils';
22

33
import { NETWORK_BODY_MAX_SIZE, WINDOW } from '../../constants';
44
import { DEBUG_BUILD } from '../../debug-build';
@@ -10,6 +10,7 @@ import type {
1010
ReplayNetworkRequestOrResponse,
1111
ReplayPerformanceEntry,
1212
} from '../../types';
13+
import { logger } from '../../util/logger';
1314

1415
/** Get the size of a body. */
1516
export function getBodySize(body: RequestInit['body']): number | undefined {
@@ -77,12 +78,12 @@ export function getBodyString(body: unknown): [string | undefined, NetworkMetaWa
7778
if (!body) {
7879
return [undefined];
7980
}
80-
} catch {
81-
DEBUG_BUILD && logger.warn('[Replay] Failed to serialize body', body);
81+
} catch (error) {
82+
DEBUG_BUILD && logger.exception(error, 'Failed to serialize body', body);
8283
return [undefined, 'BODY_PARSE_ERROR'];
8384
}
8485

85-
DEBUG_BUILD && logger.info('[Replay] Skipping network body because of body type', body);
86+
DEBUG_BUILD && logger.info('Skipping network body because of body type', body);
8687

8788
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
8889
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils';
22
import type { Breadcrumb, XhrBreadcrumbData } from '@sentry/types';
3-
import { logger } from '@sentry/utils';
43

54
import { DEBUG_BUILD } from '../../debug-build';
65
import type {
@@ -10,6 +9,7 @@ import type {
109
ReplayNetworkRequestData,
1110
XhrHint,
1211
} from '../../types';
12+
import { logger } from '../../util/logger';
1313
import { addNetworkBreadcrumb } from './addNetworkBreadcrumb';
1414
import {
1515
buildNetworkRequestOrResponse,
@@ -39,7 +39,7 @@ export async function captureXhrBreadcrumbToReplay(
3939
const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
4040
addNetworkBreadcrumb(options.replay, result);
4141
} catch (error) {
42-
DEBUG_BUILD && logger.error('[Replay] Failed to capture xhr breadcrumb', error);
42+
DEBUG_BUILD && logger.exception(error, 'Failed to capture xhr breadcrumb');
4343
}
4444
}
4545

@@ -161,7 +161,7 @@ function _getXhrResponseBody(xhr: XMLHttpRequest): [string | undefined, NetworkM
161161
errors.push(e);
162162
}
163163

164-
DEBUG_BUILD && logger.warn('[Replay] Failed to get xhr response body', ...errors);
164+
DEBUG_BUILD && logger.warn('Failed to get xhr response body', ...errors);
165165

166166
return [undefined];
167167
}
@@ -197,12 +197,12 @@ export function _parseXhrResponse(
197197
if (!body) {
198198
return [undefined];
199199
}
200-
} catch {
201-
DEBUG_BUILD && logger.warn('[Replay] Failed to serialize body', body);
200+
} catch (error) {
201+
DEBUG_BUILD && logger.exception(error, 'Failed to serialize body', body);
202202
return [undefined, 'BODY_PARSE_ERROR'];
203203
}
204204

205-
DEBUG_BUILD && logger.info('[Replay] Skipping network body because of body type', body);
205+
DEBUG_BUILD && logger.info('Skipping network body because of body type', body);
206206

207207
return [undefined, 'UNPARSEABLE_BODY_TYPE'];
208208
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { ReplayRecordingData } from '@sentry/types';
22

3-
import { logger } from '@sentry/utils';
43
import { REPLAY_MAX_EVENT_BUFFER_SIZE } from '../constants';
54
import { DEBUG_BUILD } from '../debug-build';
65
import type { AddEventResult, EventBuffer, EventBufferType, RecordingEvent } from '../types';
6+
import { logger } from '../util/logger';
77
import { timestampToMs } from '../util/timestamp';
88
import { WorkerHandler } from './WorkerHandler';
99
import { EventBufferSizeExceededError } from './error';
@@ -88,7 +88,7 @@ export class EventBufferCompressionWorker implements EventBuffer {
8888

8989
// We do not wait on this, as we assume the order of messages is consistent for the worker
9090
this._worker.postMessage('clear').then(null, e => {
91-
DEBUG_BUILD && logger.warn('[Replay] Sending "clear" message to worker failed', e);
91+
DEBUG_BUILD && logger.exception(e, 'Sending "clear" message to worker failed', e);
9292
});
9393
}
9494

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { ReplayRecordingData } from '@sentry/types';
2-
import { logger } from '@sentry/utils';
32

43
import { DEBUG_BUILD } from '../debug-build';
54
import type { AddEventResult, EventBuffer, EventBufferType, RecordingEvent } from '../types';
6-
import { logInfo } from '../util/log';
5+
import { logger } from '../util/logger';
76
import { EventBufferArray } from './EventBufferArray';
87
import { EventBufferCompressionWorker } from './EventBufferCompressionWorker';
98

@@ -90,7 +89,7 @@ export class EventBufferProxy implements EventBuffer {
9089
} catch (error) {
9190
// If the worker fails to load, we fall back to the simple buffer.
9291
// Nothing more to do from our side here
93-
logInfo('[Replay] Failed to load the compression worker, falling back to simple buffer');
92+
DEBUG_BUILD && logger.exception(error, 'Failed to load the compression worker, falling back to simple buffer');
9493
return;
9594
}
9695

@@ -117,7 +116,7 @@ export class EventBufferProxy implements EventBuffer {
117116
try {
118117
await Promise.all(addEventPromises);
119118
} catch (error) {
120-
DEBUG_BUILD && logger.warn('[Replay] Failed to add events when switching buffers.', error);
119+
DEBUG_BUILD && logger.exception(error, 'Failed to add events when switching buffers.');
121120
}
122121
}
123122
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { logger } from '@sentry/utils';
2-
31
import { DEBUG_BUILD } from '../debug-build';
42
import type { WorkerRequest, WorkerResponse } from '../types';
5-
import { logInfo } from '../util/log';
3+
import { logger } from '../util/logger';
64

75
/**
86
* Event buffer that uses a web worker to compress events.
@@ -57,7 +55,7 @@ export class WorkerHandler {
5755
* Destroy the worker.
5856
*/
5957
public destroy(): void {
60-
logInfo('[Replay] Destroying compression worker');
58+
DEBUG_BUILD && logger.info('Destroying compression worker');
6159
this._worker.terminate();
6260
}
6361

@@ -85,7 +83,7 @@ export class WorkerHandler {
8583

8684
if (!response.success) {
8785
// TODO: Do some error handling, not sure what
88-
DEBUG_BUILD && logger.error('[Replay]', response.response);
86+
DEBUG_BUILD && logger.error('Error in compression worker: ', response.response);
8987

9088
reject(new Error('Error in compression worker'));
9189
return;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getWorkerURL } from '@sentry-internal/replay-worker';
22

3+
import { DEBUG_BUILD } from '../debug-build';
34
import type { EventBuffer } from '../types';
4-
import { logInfo } from '../util/log';
5+
import { logger } from '../util/logger';
56
import { EventBufferArray } from './EventBufferArray';
67
import { EventBufferProxy } from './EventBufferProxy';
78

@@ -32,7 +33,7 @@ export function createEventBuffer({
3233
}
3334
}
3435

35-
logInfo('[Replay] Using simple buffer');
36+
DEBUG_BUILD && logger.info('Using simple buffer');
3637
return new EventBufferArray();
3738
}
3839

@@ -44,11 +45,11 @@ function _loadWorker(customWorkerUrl?: string): EventBufferProxy | void {
4445
return;
4546
}
4647

47-
logInfo(`[Replay] Using compression worker${customWorkerUrl ? ` from ${customWorkerUrl}` : ''}`);
48+
DEBUG_BUILD && logger.info(`Using compression worker${customWorkerUrl ? ` from ${customWorkerUrl}` : ''}`);
4849
const worker = new Worker(workerUrl);
4950
return new EventBufferProxy(worker);
5051
} catch (error) {
51-
logInfo('[Replay] Failed to create compression worker');
52+
DEBUG_BUILD && logger.exception(error, 'Failed to create compression worker');
5253
// Fall back to use simple event buffer array
5354
}
5455
}

0 commit comments

Comments
 (0)