@@ -3,8 +3,19 @@ import { DEBUG_BUILD } from '../debug-build';
3
3
import type { ConsoleLevel } from '../types-hoist/instrument' ;
4
4
import { GLOBAL_OBJ } from './worldwide' ;
5
5
6
- /** Prefix for logging strings */
7
- const PREFIX = 'Sentry Logger ' ;
6
+ /** A Sentry Logger instance. */
7
+ export interface Logger {
8
+ disable ( ) : void ;
9
+ enable ( ) : void ;
10
+ isEnabled ( ) : boolean ;
11
+ log ( ...args : Parameters < typeof console . log > ) : void ;
12
+ info ( ...args : Parameters < typeof console . info > ) : void ;
13
+ warn ( ...args : Parameters < typeof console . warn > ) : void ;
14
+ error ( ...args : Parameters < typeof console . error > ) : void ;
15
+ debug ( ...args : Parameters < typeof console . debug > ) : void ;
16
+ assert ( ...args : Parameters < typeof console . assert > ) : void ;
17
+ trace ( ...args : Parameters < typeof console . trace > ) : void ;
18
+ }
8
19
9
20
export const CONSOLE_LEVELS : readonly ConsoleLevel [ ] = [
10
21
'debug' ,
@@ -16,20 +27,19 @@ export const CONSOLE_LEVELS: readonly ConsoleLevel[] = [
16
27
'trace' ,
17
28
] as const ;
18
29
19
- type LoggerMethod = ( ... args : unknown [ ] ) => void ;
20
- type LoggerConsoleMethods = Record < ConsoleLevel , LoggerMethod > ;
30
+ /** Prefix for logging strings */
31
+ const PREFIX = 'Sentry Logger ' ;
21
32
22
33
/** This may be mutated by the console instrumentation. */
23
- export const originalConsoleMethods : {
24
- [ key in ConsoleLevel ] ?: ( ...args : unknown [ ] ) => void ;
25
- } = { } ;
26
-
27
- /** A Sentry Logger instance. */
28
- export interface Logger extends LoggerConsoleMethods {
29
- disable ( ) : void ;
30
- enable ( ) : void ;
31
- isEnabled ( ) : boolean ;
32
- }
34
+ export const originalConsoleMethods : Partial < {
35
+ log ( ...args : Parameters < typeof console . log > ) : void ;
36
+ info ( ...args : Parameters < typeof console . info > ) : void ;
37
+ warn ( ...args : Parameters < typeof console . warn > ) : void ;
38
+ error ( ...args : Parameters < typeof console . error > ) : void ;
39
+ debug ( ...args : Parameters < typeof console . debug > ) : void ;
40
+ assert ( ...args : Parameters < typeof console . assert > ) : void ;
41
+ trace ( ...args : Parameters < typeof console . trace > ) : void ;
42
+ } > = { } ;
33
43
34
44
/**
35
45
* Temporarily disable sentry console instrumentations.
@@ -43,60 +53,110 @@ export function consoleSandbox<T>(callback: () => T): T {
43
53
}
44
54
45
55
const console = GLOBAL_OBJ . console as Console ;
46
- const wrappedFuncs : Partial < LoggerConsoleMethods > = { } ;
56
+ const wrappedFuncs : Partial < Record < ConsoleLevel , ( ... args : unknown [ ] ) => void > > = { } ;
47
57
48
58
const wrappedLevels = Object . keys ( originalConsoleMethods ) as ConsoleLevel [ ] ;
49
59
50
60
// Restore all wrapped console methods
51
61
wrappedLevels . forEach ( level => {
52
- const originalConsoleMethod = originalConsoleMethods [ level ] as LoggerMethod ;
53
- wrappedFuncs [ level ] = console [ level ] as LoggerMethod | undefined ;
54
- console [ level ] = originalConsoleMethod ;
62
+ const originalConsoleMethod = originalConsoleMethods [ level ] ;
63
+ wrappedFuncs [ level ] = console [ level ] as ( ... args : unknown [ ] ) => void ;
64
+ console [ level ] = originalConsoleMethod as ( ... args : unknown [ ] ) => void ;
55
65
} ) ;
56
66
57
67
try {
58
68
return callback ( ) ;
59
69
} finally {
60
70
// Revert restoration to wrapped state
61
71
wrappedLevels . forEach ( level => {
62
- console [ level ] = wrappedFuncs [ level ] as LoggerMethod ;
72
+ console [ level ] = wrappedFuncs [ level ] as ( ... args : unknown [ ] ) => void ;
63
73
} ) ;
64
74
}
65
75
}
66
76
67
- function makeLogger ( ) : Logger {
68
- let enabled = false ;
69
- const logger : Partial < Logger > = {
70
- enable : ( ) => {
71
- enabled = true ;
72
- } ,
73
- disable : ( ) => {
74
- enabled = false ;
75
- } ,
76
- isEnabled : ( ) => enabled ,
77
- } ;
78
-
79
- if ( DEBUG_BUILD ) {
80
- CONSOLE_LEVELS . forEach ( name => {
81
- logger [ name ] = ( ...args : Parameters < ( typeof GLOBAL_OBJ . console ) [ typeof name ] > ) => {
82
- if ( enabled ) {
83
- consoleSandbox ( ( ) => {
84
- GLOBAL_OBJ . console [ name ] ( `${ PREFIX } [${ name } ]:` , ...args ) ;
85
- } ) ;
86
- }
87
- } ;
88
- } ) ;
89
- } else {
90
- CONSOLE_LEVELS . forEach ( name => {
91
- logger [ name ] = ( ) => undefined ;
77
+ function enable ( ) : void {
78
+ _getLoggerSettings ( ) . enabled = true ;
79
+ }
80
+
81
+ function disable ( ) : void {
82
+ _getLoggerSettings ( ) . enabled = false ;
83
+ }
84
+
85
+ function isEnabled ( ) : boolean {
86
+ return _getLoggerSettings ( ) . enabled ;
87
+ }
88
+
89
+ function log ( ...args : Parameters < typeof console . log > ) : void {
90
+ _maybeLog ( 'log' , ...args ) ;
91
+ }
92
+
93
+ function info ( ...args : Parameters < typeof console . info > ) : void {
94
+ _maybeLog ( 'info' , ...args ) ;
95
+ }
96
+
97
+ function warn ( ...args : Parameters < typeof console . warn > ) : void {
98
+ _maybeLog ( 'warn' , ...args ) ;
99
+ }
100
+
101
+ function error ( ...args : Parameters < typeof console . error > ) : void {
102
+ _maybeLog ( 'error' , ...args ) ;
103
+ }
104
+
105
+ function debug ( ...args : Parameters < typeof console . debug > ) : void {
106
+ _maybeLog ( 'debug' , ...args ) ;
107
+ }
108
+
109
+ function assert ( ...args : Parameters < typeof console . assert > ) : void {
110
+ _maybeLog ( 'assert' , ...args ) ;
111
+ }
112
+
113
+ function trace ( ...args : Parameters < typeof console . trace > ) : void {
114
+ _maybeLog ( 'trace' , ...args ) ;
115
+ }
116
+
117
+ function _maybeLog ( level : ConsoleLevel , ...args : Parameters < ( typeof console ) [ typeof level ] > ) : void {
118
+ if ( ! DEBUG_BUILD ) {
119
+ return ;
120
+ }
121
+
122
+ if ( isEnabled ( ) ) {
123
+ consoleSandbox ( ( ) => {
124
+ GLOBAL_OBJ . console [ level ] ( `${ PREFIX } [${ level } ]:` , ...args ) ;
92
125
} ) ;
93
126
}
127
+ }
128
+
129
+ function _getLoggerSettings ( ) : { enabled : boolean } {
130
+ if ( ! DEBUG_BUILD ) {
131
+ return { enabled : false } ;
132
+ }
94
133
95
- return logger as Logger ;
134
+ return getGlobalSingleton ( 'loggerSettings' , ( ) => ( { enabled : false } ) ) ;
96
135
}
97
136
98
137
/**
99
138
* This is a logger singleton which either logs things or no-ops if logging is not enabled.
100
139
* The logger is a singleton on the carrier, to ensure that a consistent logger is used throughout the SDK.
101
140
*/
102
- export const logger = getGlobalSingleton ( 'logger' , makeLogger ) ;
141
+ export const logger = {
142
+ /** Enable logging. */
143
+ enable,
144
+ /** Disable logging. */
145
+ disable,
146
+ /** Check if logging is enabled. */
147
+ isEnabled,
148
+ /** Log a message. */
149
+ log,
150
+ /** Log level info */
151
+ info,
152
+ /** Log a warning. */
153
+ warn,
154
+ /** Log an error. */
155
+ error,
156
+ /** Log a debug message. */
157
+ debug,
158
+ /** Log an assertion. */
159
+ assert,
160
+ /** Log a trace. */
161
+ trace,
162
+ } satisfies Logger ;
0 commit comments