|
1 | 1 | import { parseDebugLoggingQueryString } from '../../core/debug-mode' |
2 | 2 |
|
3 | 3 | class Logger { |
4 | | - private static loggingKey = 'segment_signals_debug_logging_enabled' |
| 4 | + private storageType = 'sessionStorage' as const |
| 5 | + private static loggingKey = 'segment_signals_debug_logging' |
| 6 | + public signalsOnly = false |
5 | 7 | constructor() { |
6 | 8 | const val = parseDebugLoggingQueryString() |
7 | 9 | if (typeof val === 'boolean') { |
8 | 10 | this.setDebugKey(Logger.loggingKey, val) |
9 | 11 | } |
10 | 12 | } |
11 | 13 |
|
12 | | - private debugLoggingEnabled(): boolean { |
| 14 | + private debugLoggingEnabled = (): boolean => { |
13 | 15 | try { |
14 | 16 | const isEnabled = Boolean( |
15 | | - window.sessionStorage.getItem(Logger.loggingKey) |
| 17 | + globalThis[this.storageType].getItem(Logger.loggingKey) |
16 | 18 | ) |
17 | 19 | if (isEnabled) { |
18 | 20 | return true |
19 | 21 | } |
20 | 22 | } catch (e) { |
21 | | - logger.debug('Storage error', e) |
| 23 | + console.warn('Storage error', e) |
22 | 24 | } |
23 | 25 | return false |
24 | 26 | } |
25 | 27 |
|
26 | | - private setDebugKey(key: string, enable: boolean) { |
| 28 | + private setDebugKey = (key: string, enable: boolean) => { |
27 | 29 | try { |
28 | 30 | if (enable) { |
29 | | - window.sessionStorage.setItem(key, 'true') |
| 31 | + globalThis[this.storageType].setItem(key, 'true') |
30 | 32 | } else { |
31 | | - logger.debug(`Removing debug key ${key} from storage`) |
32 | | - window.sessionStorage.removeItem(key) |
| 33 | + globalThis[this.storageType].removeItem(key) |
33 | 34 | } |
34 | 35 | } catch (e) { |
35 | | - logger.debug('Storage error', e) |
| 36 | + console.warn('Storage error', e) |
36 | 37 | } |
37 | 38 | } |
38 | 39 |
|
39 | | - enableDebugLogging(bool = true) { |
| 40 | + enableDebugLogging = (bool = true) => { |
40 | 41 | this.setDebugKey(Logger.loggingKey, bool) |
41 | 42 | } |
42 | 43 |
|
43 | | - debug(...args: any[]): void { |
| 44 | + debug = (...args: any[]): void => { |
44 | 45 | if (this.debugLoggingEnabled()) { |
45 | 46 | console.log('[signals debug]', ...args) |
46 | 47 | } |
|
0 commit comments