Skip to content

Commit 11cd71b

Browse files
committed
wip
1 parent 19c6ffd commit 11cd71b

File tree

7 files changed

+57
-86
lines changed

7 files changed

+57
-86
lines changed

packages/signals/signals/src/core/debug-mode/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ export const parseDebugModeQueryString = (): boolean | undefined => {
1717
/**
1818
* This turns on advanced logging for signals!
1919
*/
20-
export type LogLevelOptions = 'info' | 'debug' | 'off' | undefined
21-
export const parseSignalsLogLevel = (): LogLevelOptions => {
20+
export type LogLevelOptions = 'info' | 'debug' | 'off'
21+
export const parseSignalsLogLevel = (): LogLevelOptions | undefined => {
2222
const queryParams = new URLSearchParams(window.location.search)
2323

2424
const val =
2525
queryParams.get('segment_signals_log_level') ||
2626
queryParams.get('seg_signals_log_level')
2727
if (val === 'info' || val === 'debug' || val === 'off') {
2828
return val
29-
} else if (typeof val === undefined) {
30-
return undefined
31-
} else {
29+
} else if (typeof val === 'string') {
3230
console.error(
3331
`Invalid signals_log_level: "${val}". Valid options are: info, debug, off`
3432
)
3533
}
34+
return undefined
3635
}

packages/signals/signals/src/core/signals/settings.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SignalsIngestSettingsConfig } from '../client'
55
import { SandboxSettingsConfig } from '../processor/sandbox'
66
import { NetworkSettingsConfig } from '../signal-generators/network-gen'
77
import { SignalsPluginSettingsConfig } from '../../types'
8-
import { DebugStorage } from '../../lib/storage/debug-storage'
8+
import { WebStorage } from '../../lib/storage/web-storage'
99

1010
export type SignalsSettingsConfig = Pick<
1111
SignalsPluginSettingsConfig,
@@ -114,21 +114,15 @@ export class SignalGlobalSettings {
114114
export class SignalsDebugSettings {
115115
private static redactionKey = 'segment_signals_debug_redaction_disabled'
116116
private static ingestionKey = 'segment_signals_debug_ingestion_enabled'
117-
storage: DebugStorage
117+
storage: WebStorage
118118

119119
constructor(disableRedaction?: boolean, enableIngestion?: boolean) {
120-
this.storage = new DebugStorage('sessionStorage')
120+
this.storage = new WebStorage(window.sessionStorage)
121121
if (typeof disableRedaction === 'boolean') {
122-
this.storage.setDebugKey(
123-
SignalsDebugSettings.redactionKey,
124-
disableRedaction
125-
)
122+
this.storage.setItem(SignalsDebugSettings.redactionKey, disableRedaction)
126123
}
127124
if (typeof enableIngestion === 'boolean') {
128-
this.storage.setDebugKey(
129-
SignalsDebugSettings.ingestionKey,
130-
enableIngestion
131-
)
125+
this.storage.setItem(SignalsDebugSettings.ingestionKey, enableIngestion)
132126
}
133127

134128
const debugModeInQs = parseDebugModeQueryString()
@@ -139,15 +133,15 @@ export class SignalsDebugSettings {
139133
}
140134

141135
setAllDebugging = (boolean: boolean) => {
142-
this.storage.setDebugKey(SignalsDebugSettings.redactionKey, boolean)
143-
this.storage.setDebugKey(SignalsDebugSettings.ingestionKey, boolean)
136+
this.storage.setItem(SignalsDebugSettings.redactionKey, boolean)
137+
this.storage.setItem(SignalsDebugSettings.ingestionKey, boolean)
144138
}
145139

146140
getDisableSignalsRedaction = (): boolean => {
147-
return this.storage.getDebugKey(SignalsDebugSettings.redactionKey)
141+
return this.storage.getItem(SignalsDebugSettings.redactionKey) ?? false
148142
}
149143

150144
getEnableSignalsIngestion = (): boolean => {
151-
return this.storage.getDebugKey(SignalsDebugSettings.ingestionKey)
145+
return this.storage.getItem(SignalsDebugSettings.ingestionKey) ?? false
152146
}
153147
}

packages/signals/signals/src/core/signals/signals.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { SignalEventProcessor } from '../processor/processor'
1515
import { Sandbox, SandboxSettings } from '../processor/sandbox'
1616
import { SignalGlobalSettings, SignalsSettingsConfig } from './settings'
1717
import { logger } from '../../lib/logger'
18+
import { LogLevelOptions } from '../debug-mode'
1819

1920
interface ISignals {
2021
start(analytics: AnyAnalytics): Promise<void>
@@ -135,9 +136,9 @@ export class Signals implements ISignals {
135136
/**
136137
* Disable redaction, ingestion of signals, and other logging.
137138
*/
138-
debug(boolean = true): void {
139+
debug(boolean = true, logLevel?: LogLevelOptions): void {
139140
this.globalSettings.signalsDebug.setAllDebugging(boolean)
140-
logger.enableLogging('info')
141+
logger.enableLogging(logLevel ?? 'info')
141142
}
142143

143144
/**

packages/signals/signals/src/lib/logger/index.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import {
2+
LogLevelOptions,
23
parseDebugModeQueryString,
34
parseSignalsLogLevel,
45
} from '../../core/debug-mode'
5-
import { DebugStorage } from '../storage/debug-storage'
6+
import { WebStorage } from '../storage/web-storage'
67

78
class Logger {
8-
private static infoLogging = 'segment_signals_log_level_info'
9-
private static debugLogging = 'segment_signals_log_level_debug'
10-
11-
storage = new DebugStorage('sessionStorage')
9+
private logLevelKey = 'segment_signals_log_level'
10+
get logLevel(): LogLevelOptions {
11+
return this.storage.getItem(this.logLevelKey) ?? 'off'
12+
}
13+
storage = new WebStorage(window.sessionStorage)
1214

1315
constructor() {
1416
// if debug mode is in the query string, we want simple logging
@@ -24,42 +26,23 @@ class Logger {
2426
}
2527
}
2628

27-
private loggingEnabled = (): boolean => {
28-
return this.storage.getDebugKey(Logger.infoLogging)
29-
}
30-
31-
private debugLoggingEnabled = (): boolean => {
32-
return this.storage.getDebugKey(Logger.debugLogging)
33-
}
34-
35-
enableDebugLogging = (bool = true) => {
36-
this.storage.setDebugKey(Logger.debugLogging, bool)
37-
}
38-
3929
// 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-
}
30+
enableLogging = (type: LogLevelOptions) => {
31+
this.storage.setItem(this.logLevelKey, type)
4832
}
4933

5034
disableLogging = () => {
51-
this.storage.setDebugKey(Logger.infoLogging, false)
52-
this.storage.setDebugKey(Logger.debugLogging, false)
35+
this.storage.setItem(this.logLevelKey, 'off')
5336
}
5437

5538
info = (...args: any[]): void => {
56-
if (this.loggingEnabled() || this.debugLoggingEnabled()) {
39+
if (this.logLevel === 'info' || this.logLevel === 'debug') {
5740
console.log('[signals log]', ...args)
5841
}
5942
}
6043

6144
debug = (...args: any[]): void => {
62-
if (this.debugLoggingEnabled()) {
45+
if (this.logLevel === 'debug') {
6346
console.log('[signals debug]', ...args)
6447
}
6548
}

packages/signals/signals/src/lib/storage/debug-storage.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export class WebStorage {
2+
private storage: Storage
3+
constructor(storage: Storage) {
4+
this.storage = storage
5+
}
6+
7+
public setItem = (key: string, value: string | boolean): void => {
8+
try {
9+
this.storage.setItem(key, value.toString())
10+
} catch (e) {
11+
console.warn('Storage error', e)
12+
}
13+
}
14+
15+
public getItem = <T>(key: string): T | undefined => {
16+
try {
17+
return (this.storage.getItem(key) as T) ?? undefined
18+
} catch (e) {
19+
console.warn('Storage error', e)
20+
}
21+
return undefined
22+
}
23+
}

packages/signals/signals/src/plugin/signals-plugin.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
2929
constructor(settings: SignalsPluginSettingsConfig = {}) {
3030
assertBrowserEnv()
3131

32-
// assign to window for debugging purposes
33-
Object.assign(window, { SegmentSignalsPlugin: this })
34-
3532
if (settings.enableDebugLogging) {
36-
logger.enableDebugLogging()
33+
logger.enableLogging('debug')
3734
}
3835

3936
logger.debug(`SignalsPlugin v${version} initializing`, {
@@ -57,6 +54,9 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
5754
networkSignalsDisallowList: settings.networkSignalsDisallowList,
5855
signalStorage: settings.signalStorage,
5956
})
57+
58+
// assign to window for debugging purposes
59+
Object.assign(window, { SegmentSignalsPlugin: this })
6060
}
6161

6262
isLoaded() {
@@ -89,7 +89,7 @@ export class SignalsPlugin implements Plugin, SignalsAugmentedFunctionality {
8989
/**
9090
* Enable redaction and disable ingestion of signals. Also, logs signals to the console.
9191
*/
92-
debug(boolean = true): void {
93-
this.signals.debug(boolean)
92+
debug(...args: Parameters<typeof this.signals['debug']>): void {
93+
this.signals.debug(...args)
9494
}
9595
}

0 commit comments

Comments
 (0)