@@ -6,9 +6,15 @@ import { DEBUG_BUILD } from '../debug-build';
66
77type ReplayConsoleLevels = Extract < ConsoleLevel , 'info' | 'warn' | 'error' | 'log' > ;
88const CONSOLE_LEVELS : readonly ReplayConsoleLevels [ ] = [ 'info' , 'warn' , 'error' , 'log' ] as const ;
9+ const PREFIX = '[Replay] ' ;
910
1011type 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 */
1420interface 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 } ;
0 commit comments