Skip to content

Commit 7954cf3

Browse files
authored
fix(replay): Streamline logger usage (#6305)
1 parent 23c7575 commit 7954cf3

File tree

7 files changed

+32
-60
lines changed

7 files changed

+32
-60
lines changed

packages/replay/jest.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ export default async (): Promise<Config.InitialOptions> => {
1313
setupFilesAfterEnv: ['./jest.setup.ts'],
1414
testEnvironment: 'jsdom',
1515
testMatch: ['<rootDir>/test/**/*(*.)@(spec|test).ts'],
16+
globals: {
17+
'ts-jest': {
18+
tsconfig: '<rootDir>/tsconfig.json',
19+
},
20+
__DEBUG_BUILD__: true,
21+
},
1622
};
1723
};

packages/replay/src/eventBuffer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// TODO: figure out member access types and remove the line above
33

44
import { captureException } from '@sentry/core';
5+
import { logger } from '@sentry/utils';
56

67
import { RecordingEvent, WorkerRequest, WorkerResponse } from './types';
7-
import { logger } from './util/logger';
88
import workerString from './worker/worker.js';
99

1010
interface CreateEventBufferParams {
@@ -17,7 +17,7 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
1717
const workerUrl = URL.createObjectURL(workerBlob);
1818

1919
try {
20-
logger.log('Using compression worker');
20+
__DEBUG_BUILD__ && logger.log('[Replay] Using compression worker');
2121
const worker = new Worker(workerUrl);
2222
if (worker) {
2323
return new EventBufferCompressionWorker(worker);
@@ -27,10 +27,10 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
2727
} catch {
2828
// catch and ignore, fallback to simple event buffer
2929
}
30-
logger.log('Falling back to simple event buffer');
30+
__DEBUG_BUILD__ && logger.log('[Replay] Falling back to simple event buffer');
3131
}
3232

33-
logger.log('Using simple buffer');
33+
__DEBUG_BUILD__ && logger.log('[Replay] Using simple buffer');
3434
return new EventBufferArray();
3535
}
3636

@@ -116,7 +116,7 @@ export class EventBufferCompressionWorker implements IEventBuffer {
116116

117117
if (!data.success) {
118118
// TODO: Do some error handling, not sure what
119-
logger.error(data.response);
119+
__DEBUG_BUILD__ && logger.error('[Replay]', data.response);
120120

121121
reject(new Error('Error in compression worker'));
122122
return;
@@ -142,11 +142,11 @@ export class EventBufferCompressionWorker implements IEventBuffer {
142142

143143
init(): void {
144144
this.postMessage({ id: this.id, method: 'init', args: [] });
145-
logger.log('Initialized compression worker');
145+
__DEBUG_BUILD__ && logger.log('[Replay] Initialized compression worker');
146146
}
147147

148148
destroy(): void {
149-
logger.log('Destroying compression worker');
149+
__DEBUG_BUILD__ && logger.log('[Replay] Destroying compression worker');
150150
this.worker?.terminate();
151151
this.worker = null;
152152
}

packages/replay/src/flags.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/replay/src/index.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */ // TODO: We might want to split this file up
22
import { addGlobalEventProcessor, getCurrentHub, Scope, setContext } from '@sentry/core';
33
import { Breadcrumb, Client, Event, Integration } from '@sentry/types';
4-
import { addInstrumentationHandler, createEnvelope } from '@sentry/utils';
4+
import { addInstrumentationHandler, createEnvelope, logger } from '@sentry/utils';
55
import debounce from 'lodash.debounce';
66
import { PerformanceObserverEntryList } from 'perf_hooks';
77
import { EventType, record } from 'rrweb';
@@ -40,7 +40,6 @@ import { createPayload } from './util/createPayload';
4040
import { dedupePerformanceEntries } from './util/dedupePerformanceEntries';
4141
import { isExpired } from './util/isExpired';
4242
import { isSessionExpired } from './util/isSessionExpired';
43-
import { logger } from './util/logger';
4443

4544
/**
4645
* Returns true to return control to calling function, otherwise continue with normal batching
@@ -300,7 +299,7 @@ export class Replay implements Integration {
300299
emit: this.handleRecordingEmit,
301300
});
302301
} catch (err) {
303-
logger.error(err);
302+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
304303
captureInternalException(err);
305304
}
306305
}
@@ -315,14 +314,14 @@ export class Replay implements Integration {
315314
}
316315

317316
try {
318-
logger.log('Stopping Replays');
317+
__DEBUG_BUILD__ && logger.log('[Replay] Stopping Replays');
319318
this.isEnabled = false;
320319
this.removeListeners();
321320
this.stopRecording?.();
322321
this.eventBuffer?.destroy();
323322
this.eventBuffer = null;
324323
} catch (err) {
325-
logger.error(err);
324+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
326325
captureInternalException(err);
327326
}
328327
}
@@ -340,7 +339,7 @@ export class Replay implements Integration {
340339
this.stopRecording = undefined;
341340
}
342341
} catch (err) {
343-
logger.error(err);
342+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
344343
captureInternalException(err);
345344
}
346345
}
@@ -361,7 +360,7 @@ export class Replay implements Integration {
361360
deleteSession();
362361
this.session = undefined;
363362
} catch (err) {
364-
logger.error(err);
363+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
365364
captureInternalException(err);
366365
}
367366
}
@@ -437,7 +436,7 @@ export class Replay implements Integration {
437436
this.hasInitializedCoreListeners = true;
438437
}
439438
} catch (err) {
440-
logger.error(err);
439+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
441440
captureInternalException(err);
442441
}
443442

@@ -487,7 +486,7 @@ export class Replay implements Integration {
487486
this.performanceObserver = null;
488487
}
489488
} catch (err) {
490-
logger.error(err);
489+
__DEBUG_BUILD__ && logger.error('[Replay]', err);
491490
captureInternalException(err);
492491
}
493492
}
@@ -599,7 +598,7 @@ export class Replay implements Integration {
599598
) => {
600599
// If this is false, it means session is expired, create and a new session and wait for checkout
601600
if (!this.checkAndHandleExpiredSession()) {
602-
logger.error(new Error('Received replay event after session expired.'));
601+
__DEBUG_BUILD__ && logger.error('[Replay] Received replay event after session expired.');
603602

604603
return;
605604
}
@@ -827,7 +826,7 @@ export class Replay implements Integration {
827826
// If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
828827
// ms, we will re-use the existing session, otherwise create a new
829828
// session
830-
logger.log('Document has become active, but session has expired');
829+
__DEBUG_BUILD__ && logger.log('[Replay] Document has become active, but session has expired');
831830
return;
832831
}
833832

@@ -841,7 +840,7 @@ export class Replay implements Integration {
841840
* create a new Replay event.
842841
*/
843842
triggerFullSnapshot(): void {
844-
logger.log('Taking full rrweb snapshot');
843+
__DEBUG_BUILD__ && logger.log('[Replay] Taking full rrweb snapshot');
845844
record.takeFullSnapshot(true);
846845
}
847846

@@ -1131,12 +1130,12 @@ export class Replay implements Integration {
11311130
}
11321131

11331132
if (!this.checkAndHandleExpiredSession()) {
1134-
logger.error(new Error('Attempting to finish replay event after session expired.'));
1133+
__DEBUG_BUILD__ && logger.error('[Replay] Attempting to finish replay event after session expired.');
11351134
return;
11361135
}
11371136

11381137
if (!this.session?.id) {
1139-
console.error(new Error('[Sentry]: No transaction, no replay'));
1138+
console.error('[Replay]: No transaction, no replay');
11401139
return;
11411140
}
11421141

packages/replay/src/session/createSession.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { logger } from '@sentry/utils';
2+
13
import { SessionOptions } from '../types';
2-
import { logger } from '../util/logger';
34
import { saveSession } from './saveSession';
45
import { Session } from './Session';
56

@@ -15,7 +16,7 @@ export function createSession({ sessionSampleRate, errorSampleRate, stickySessio
1516
sessionSampleRate,
1617
});
1718

18-
logger.log(`Creating new session: ${session.id}`);
19+
__DEBUG_BUILD__ && logger.log(`[Replay] Creating new session: ${session.id}`);
1920

2021
if (stickySession) {
2122
saveSession(session);

packages/replay/src/session/getSession.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { logger } from '@sentry/utils';
2+
13
import { SessionOptions } from '../types';
24
import { isSessionExpired } from '../util/isSessionExpired';
3-
import { logger } from '../util/logger';
45
import { createSession } from './createSession';
56
import { fetchSession } from './fetchSession';
67
import { Session } from './Session';
@@ -39,7 +40,7 @@ export function getSession({
3940
if (!isExpired) {
4041
return { type: 'saved', session };
4142
} else {
42-
logger.log('Session has expired');
43+
__DEBUG_BUILD__ && logger.log('[Replay] Session has expired');
4344
}
4445
// Otherwise continue to create a new session
4546
}

packages/replay/src/util/logger.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)