Skip to content

Commit 11415f5

Browse files
committed
refactor
1 parent 885e312 commit 11415f5

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

packages/replay-internal/src/replay.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,11 @@ export class ReplayContainer implements ReplayContainerInterface {
206206
}
207207

208208
// Configure replay logger w/ experimental options
209-
if (this._options._experiments.traceInternals) {
210-
logger.enableTraceInternals();
211-
}
212-
213-
if (this._options._experiments.captureExceptions) {
214-
logger.enableCaptureInternalExceptions();
215-
}
209+
const experiments = options._experiments;
210+
logger.setConfig({
211+
captureExceptions: !!experiments.captureExceptions,
212+
traceInternals: !!experiments.traceInternals,
213+
});
216214
}
217215

218216
/** Get the event context. */

packages/replay-internal/src/util/logger.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import { DEBUG_BUILD } from '../debug-build';
66

77
type ReplayConsoleLevels = Extract<ConsoleLevel, 'info' | 'warn' | 'error' | 'log'>;
88
const CONSOLE_LEVELS: readonly ReplayConsoleLevels[] = ['info', 'warn', 'error', 'log'] as const;
9+
const PREFIX = '[Replay] ';
910

1011
type LoggerMethod = (...args: unknown[]) => void;
11-
type LoggerConsoleMethods = Record<'info' | 'warn' | 'error' | 'log', LoggerMethod>;
12+
type LoggerConsoleMethods = Record<ReplayConsoleLevels, LoggerMethod>;
13+
14+
interface LoggerConfig {
15+
captureExceptions: boolean;
16+
traceInternals: boolean;
17+
}
1218

1319
/** JSDoc */
1420
interface ReplayLogger extends LoggerConsoleMethods {
@@ -21,13 +27,18 @@ interface ReplayLogger extends LoggerConsoleMethods {
2127
* Captures exceptions (`Error`) if "capture internal exceptions" is enabled
2228
*/
2329
exception: LoggerMethod;
24-
enableCaptureInternalExceptions(): void;
25-
disableCaptureInternalExceptions(): void;
26-
enableTraceInternals(): void;
27-
disableTraceInternals(): void;
30+
/**
31+
* Configures the logger with additional debugging behavior
32+
*/
33+
setConfig(config: LoggerConfig): void;
2834
}
2935

30-
function _addBreadcrumb(message: string, level: SeverityLevel = 'info'): void {
36+
function _addBreadcrumb(message: unknown, level: SeverityLevel = 'info'): void {
37+
// Only support strings for breadcrumbs
38+
if (typeof message !== 'string') {
39+
return;
40+
}
41+
3142
// Wait a tick here to avoid race conditions for some initial logs
3243
// which may be added before replay is initialized
3344
addBreadcrumb(
@@ -37,7 +48,7 @@ function _addBreadcrumb(message: string, level: SeverityLevel = 'info'): void {
3748
logger: 'replay',
3849
},
3950
level,
40-
message: `[Replay] ${message}`,
51+
message: `${PREFIX} ${message}`,
4152
},
4253
{ level },
4354
);
@@ -50,46 +61,36 @@ function makeReplayLogger(): ReplayLogger {
5061
const _logger: Partial<ReplayLogger> = {
5162
exception: () => undefined,
5263
infoTick: () => undefined,
53-
enableCaptureInternalExceptions: () => {
54-
_capture = true;
55-
},
56-
disableCaptureInternalExceptions: () => {
57-
_capture = false;
58-
},
59-
enableTraceInternals: () => {
60-
_trace = true;
61-
},
62-
disableTraceInternals: () => {
63-
_trace = false;
64+
setConfig: (opts: LoggerConfig) => {
65+
_capture = opts.captureExceptions;
66+
_trace = opts.traceInternals;
6467
},
6568
};
6669

6770
if (DEBUG_BUILD) {
6871
_logger.exception = (error: unknown) => {
69-
coreLogger.error('[Replay] ', error);
72+
coreLogger.error(PREFIX, error);
7073

7174
if (_capture) {
7275
captureException(error);
73-
}
74-
75-
// No need for a breadcrumb is `_capture` is enabled since it should be
76-
// captured as an exception
77-
if (_trace && !_capture) {
76+
} else if (_trace) {
77+
// No need for a breadcrumb is `_capture` is enabled since it should be
78+
// captured as an exception
7879
_addBreadcrumb(error instanceof Error ? error.message : 'Unknown error');
7980
}
8081
};
8182

8283
_logger.infoTick = (...args: unknown[]) => {
83-
coreLogger.info('[Replay] ', ...args);
84+
coreLogger.info(PREFIX, ...args);
8485
if (_trace) {
85-
setTimeout(() => typeof args[0] === 'string' && _addBreadcrumb(args[0]), 0);
86+
setTimeout(() => _addBreadcrumb(args[0]), 0);
8687
}
8788
};
8889

8990
CONSOLE_LEVELS.forEach(name => {
9091
_logger[name] = (...args: unknown[]) => {
91-
coreLogger[name]('[Replay] ', ...args);
92-
if (_trace && typeof args[0] === 'string') {
92+
coreLogger[name](PREFIX, ...args);
93+
if (_trace) {
9394
_addBreadcrumb(args[0]);
9495
}
9596
};

packages/replay-internal/test/integration/flush.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ describe('Integration | flush', () => {
336336
});
337337

338338
it('logs warning if flushing initial segment without checkout', async () => {
339-
// replay.getOptions()._experiments.traceInternals = true;
340-
logger.enableTraceInternals();
339+
logger.setConfig({ traceInternals: true });
341340

342341
sessionStorage.clear();
343342
clearSession(replay);
@@ -410,11 +409,11 @@ describe('Integration | flush', () => {
410409
},
411410
]);
412411

413-
logger.disableTraceInternals();
412+
logger.setConfig({ traceInternals: false });
414413
});
415414

416415
it('logs warning if adding event that is after maxReplayDuration', async () => {
417-
logger.enableTraceInternals();
416+
logger.setConfig({ traceInternals: true });
418417

419418
const spyLogger = vi.spyOn(SentryUtils.logger, 'info');
420419

@@ -448,7 +447,7 @@ describe('Integration | flush', () => {
448447
} because it is after maxReplayDuration`,
449448
);
450449

451-
logger.disableTraceInternals();
450+
logger.setConfig({ traceInternals: false });
452451
spyLogger.mockRestore();
453452
});
454453

0 commit comments

Comments
 (0)