Skip to content

Commit 19c6ffd

Browse files
committed
refactor log levels
1 parent 8616fc8 commit 19c6ffd

File tree

7 files changed

+67
-40
lines changed

7 files changed

+67
-40
lines changed

.changeset/shiny-boats-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-signals': patch
3+
---
4+
5+
Add signals logging for events

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

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

2524
const val =
26-
queryParams.get('segment_signals_logging_advanced') ||
27-
queryParams.get('seg_signals_logging_advanced')
28-
if (val === 'true' || val === 'false') {
29-
return val === 'true'
25+
queryParams.get('segment_signals_log_level') ||
26+
queryParams.get('seg_signals_log_level')
27+
if (val === 'info' || val === 'debug' || val === 'off') {
28+
return val
29+
} else if (typeof val === undefined) {
30+
return undefined
31+
} else {
32+
console.error(
33+
`Invalid signals_log_level: "${val}". Valid options are: info, debug, off`
34+
)
3035
}
31-
return undefined
3236
}

packages/signals/signals/src/core/emitter/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@ export interface EmitSignal {
66
emit: (signal: Signal) => void
77
}
88

9-
interface SignalEmitterSettings {
10-
shouldLogSignals: () => boolean
11-
}
12-
139
export class SignalEmitter implements EmitSignal {
1410
private emitter = new Emitter<{ add: [Signal] }>()
1511
private listeners = new Set<(signal: Signal) => void>()
16-
private settings?: SignalEmitterSettings
17-
constructor(settings?: SignalEmitterSettings) {
18-
this.settings = settings
19-
}
12+
2013
emit(signal: Signal) {
21-
if (this.settings?.shouldLogSignals()) {
22-
logger.log('New signal:', signal.type, signal.data)
23-
}
14+
logger.info('New signal:', signal.type, signal.data)
2415
this.emitter.emit('add', signal)
2516
}
2617

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export class SignalEventProcessor {
1818
const name = methodName as MethodName
1919
const eventsCollection = analyticsMethodCalls[name]
2020
eventsCollection.forEach((args) => {
21-
logger.debug(`analytics.${name}(...) called with args`, args)
21+
logger.info('New signals-originated event:', name, { args })
22+
2223
// @ts-ignore
2324
this.analytics[name](...args)
2425
})

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ 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-
private static logSignals = 'segment_signals_log_signals_enabled'
118117
storage: DebugStorage
119118

120119
constructor(disableRedaction?: boolean, enableIngestion?: boolean) {
@@ -142,7 +141,6 @@ export class SignalsDebugSettings {
142141
setAllDebugging = (boolean: boolean) => {
143142
this.storage.setDebugKey(SignalsDebugSettings.redactionKey, boolean)
144143
this.storage.setDebugKey(SignalsDebugSettings.ingestionKey, boolean)
145-
this.storage.setDebugKey(SignalsDebugSettings.logSignals, boolean)
146144
}
147145

148146
getDisableSignalsRedaction = (): boolean => {
@@ -152,8 +150,4 @@ export class SignalsDebugSettings {
152150
getEnableSignalsIngestion = (): boolean => {
153151
return this.storage.getDebugKey(SignalsDebugSettings.ingestionKey)
154152
}
155-
156-
getEnableLogSignals = (): boolean => {
157-
return this.storage.getDebugKey(SignalsDebugSettings.logSignals)
158-
}
159153
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ export class Signals implements ISignals {
3838
private globalSettings: SignalGlobalSettings
3939
constructor(settingsConfig: SignalsSettingsConfig = {}) {
4040
this.globalSettings = new SignalGlobalSettings(settingsConfig)
41-
this.signalEmitter = new SignalEmitter({
42-
shouldLogSignals: () =>
43-
this.globalSettings.signalsDebug.getEnableLogSignals(),
44-
})
41+
this.signalEmitter = new SignalEmitter()
4542
this.signalsClient = new SignalsIngestClient(
4643
this.globalSettings.ingestClient
4744
)
@@ -136,10 +133,11 @@ export class Signals implements ISignals {
136133
}
137134

138135
/**
139-
* Disable redaction, ingestion of signals, and other debug logging.
136+
* Disable redaction, ingestion of signals, and other logging.
140137
*/
141138
debug(boolean = true): void {
142139
this.globalSettings.signalsDebug.setAllDebugging(boolean)
140+
logger.enableLogging('info')
143141
}
144142

145143
/**

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
1-
import { parseSignalsLoggingAdvancedQueryString } from '../../core/debug-mode'
1+
import {
2+
parseDebugModeQueryString,
3+
parseSignalsLogLevel,
4+
} from '../../core/debug-mode'
25
import { DebugStorage } from '../storage/debug-storage'
36

47
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'
610

711
storage = new DebugStorage('sessionStorage')
12+
813
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)
1224
}
1325
}
1426

27+
private loggingEnabled = (): boolean => {
28+
return this.storage.getDebugKey(Logger.infoLogging)
29+
}
30+
1531
private debugLoggingEnabled = (): boolean => {
16-
return this.storage.getDebugKey(Logger.advancedLogging)
32+
return this.storage.getDebugKey(Logger.debugLogging)
1733
}
1834

1935
enableDebugLogging = (bool = true) => {
20-
this.storage.setDebugKey(Logger.advancedLogging, bool)
36+
this.storage.setDebugKey(Logger.debugLogging, bool)
2137
}
2238

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+
}
2559
}
2660

2761
debug = (...args: any[]): void => {

0 commit comments

Comments
 (0)