|
1 | | -import { parseSignalsLoggingAdvancedQueryString } from '../../core/debug-mode' |
| 1 | +import { |
| 2 | + parseDebugModeQueryString, |
| 3 | + parseSignalsLogLevel, |
| 4 | +} from '../../core/debug-mode' |
2 | 5 | import { DebugStorage } from '../storage/debug-storage' |
3 | 6 |
|
4 | 7 | class Logger { |
5 | | - private static advancedLogging = 'segment_signals_logging_advanced' |
| 8 | + private static infoLogging = 'segment_signals_log_level_info' |
| 9 | + private static debugLogging = 'segment_signals_log_level_debug' |
6 | 10 |
|
7 | 11 | storage = new DebugStorage('sessionStorage') |
| 12 | + |
8 | 13 | constructor() { |
9 | | - const val = parseSignalsLoggingAdvancedQueryString() |
10 | | - if (typeof val === 'boolean') { |
11 | | - this.storage.setDebugKey(Logger.advancedLogging, val) |
| 14 | + // if debug mode is in the query string, we want simple logging |
| 15 | + const debugMode = parseDebugModeQueryString() |
| 16 | + if (typeof debugMode === 'boolean') { |
| 17 | + this.enableLogging('info') |
| 18 | + } |
| 19 | + |
| 20 | + // if log level is set to 'off' / 'log' / 'debug' in the query string, we want to set the write key |
| 21 | + const logLevel = parseSignalsLogLevel() |
| 22 | + if (logLevel !== undefined) { |
| 23 | + logLevel === 'off' ? this.disableLogging() : this.enableLogging(logLevel) |
12 | 24 | } |
13 | 25 | } |
14 | 26 |
|
| 27 | + private loggingEnabled = (): boolean => { |
| 28 | + return this.storage.getDebugKey(Logger.infoLogging) |
| 29 | + } |
| 30 | + |
15 | 31 | private debugLoggingEnabled = (): boolean => { |
16 | | - return this.storage.getDebugKey(Logger.advancedLogging) |
| 32 | + return this.storage.getDebugKey(Logger.debugLogging) |
17 | 33 | } |
18 | 34 |
|
19 | 35 | enableDebugLogging = (bool = true) => { |
20 | | - this.storage.setDebugKey(Logger.advancedLogging, bool) |
| 36 | + this.storage.setDebugKey(Logger.debugLogging, bool) |
21 | 37 | } |
22 | 38 |
|
23 | | - log = (...args: any[]): void => { |
24 | | - console.log('[signals log]', ...args) |
| 39 | + // if debug level is enabled, info level is also enabled |
| 40 | + enableLogging = (type: 'info' | 'debug') => { |
| 41 | + if (type === 'info') { |
| 42 | + this.storage.setDebugKey(Logger.infoLogging, true) |
| 43 | + this.storage.setDebugKey(Logger.debugLogging, false) |
| 44 | + } else if (type === 'debug') { |
| 45 | + this.storage.setDebugKey(Logger.debugLogging, true) |
| 46 | + this.storage.setDebugKey(Logger.infoLogging, true) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + disableLogging = () => { |
| 51 | + this.storage.setDebugKey(Logger.infoLogging, false) |
| 52 | + this.storage.setDebugKey(Logger.debugLogging, false) |
| 53 | + } |
| 54 | + |
| 55 | + info = (...args: any[]): void => { |
| 56 | + if (this.loggingEnabled() || this.debugLoggingEnabled()) { |
| 57 | + console.log('[signals log]', ...args) |
| 58 | + } |
25 | 59 | } |
26 | 60 |
|
27 | 61 | debug = (...args: any[]): void => { |
|
0 commit comments