diff --git a/posthog-core/src/index.ts b/posthog-core/src/index.ts index d39be590..38ef51ce 100644 --- a/posthog-core/src/index.ts +++ b/posthog-core/src/index.ts @@ -113,11 +113,15 @@ export abstract class PostHogCoreStateless { this._isInitialized = true } + protected logMsgIfDebug(fn: () => void): void { + if (this.isDebug) { + fn() + } + } + protected wrap(fn: () => void): void { if (this.disabled) { - if (this.isDebug) { - console.warn('[PostHog] The client is disabled') - } + this.logMsgIfDebug(() => console.warn('[PostHog] The client is disabled')) return } @@ -500,7 +504,7 @@ export abstract class PostHogCoreStateless { if (queue.length >= this.maxQueueSize) { queue.shift() - console.info('Queue is full, the oldest event is dropped.') + this.logMsgIfDebug(() => console.info('Queue is full, the oldest event is dropped.')) } queue.push({ message }) @@ -695,7 +699,7 @@ export abstract class PostHogCoreStateless { if (!isPostHogFetchError(e)) { throw e } - console.error('Error while shutting down PostHog', e) + this.logMsgIfDebug(() => console.error('Error while shutting down PostHog', e)) } } } @@ -1167,9 +1171,11 @@ export abstract class PostHogCore extends PostHogCoreStateless { const sessionReplay = res?.sessionRecording if (sessionReplay) { this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, sessionReplay) - console.log('PostHog Debug', 'Session replay config: ', JSON.stringify(sessionReplay)) + this.logMsgIfDebug(() => + console.log('PostHog Debug', 'Session replay config: ', JSON.stringify(sessionReplay)) + ) } else { - console.info('PostHog Debug', 'Session replay config disabled.') + this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay config disabled.')) this.setPersistedProperty(PostHogPersistedProperty.SessionReplay, null) } } @@ -1310,7 +1316,7 @@ export abstract class PostHogCore extends PostHogCoreStateless { .catch((e) => { cb?.(e, undefined) if (!cb) { - console.log('[PostHog] Error reloading feature flags', e) + this.logMsgIfDebug(() => console.log('[PostHog] Error reloading feature flags', e)) } }) } diff --git a/posthog-node/CHANGELOG.md b/posthog-node/CHANGELOG.md index a3385f24..aaefd5d2 100644 --- a/posthog-node/CHANGELOG.md +++ b/posthog-node/CHANGELOG.md @@ -1,5 +1,9 @@ # Next +# 4.2.1 - 2024-10-14 + +1. fix: only log messages if debug is enabled + # 4.2.0 - 2024-08-26 1. Added `historicalMigration` option for use in tools that are migrating large data to PostHog diff --git a/posthog-node/package.json b/posthog-node/package.json index ad304d43..bceb9a35 100644 --- a/posthog-node/package.json +++ b/posthog-node/package.json @@ -1,6 +1,6 @@ { "name": "posthog-node", - "version": "4.2.0", + "version": "4.2.1", "description": "PostHog Node.js integration", "repository": { "type": "git", diff --git a/posthog-node/src/feature-flags.ts b/posthog-node/src/feature-flags.ts index 45bb33c6..09b6eb5b 100644 --- a/posthog-node/src/feature-flags.ts +++ b/posthog-node/src/feature-flags.ts @@ -90,6 +90,12 @@ class FeatureFlagsPoller { this.debugMode = enabled } + private logMsgIfDebug(fn: () => void): void { + if (this.debugMode) { + fn() + } + } + async getFeatureFlag( key: string, distinctId: string, @@ -99,7 +105,7 @@ class FeatureFlagsPoller { ): Promise { await this.loadFeatureFlags() - let response = undefined + let response: string | boolean | undefined = undefined let featureFlag = undefined if (!this.loadedSuccessfullyOnce) { @@ -116,14 +122,10 @@ class FeatureFlagsPoller { if (featureFlag !== undefined) { try { response = this.computeFlagLocally(featureFlag, distinctId, groups, personProperties, groupProperties) - if (this.debugMode) { - console.debug(`Successfully computed flag locally: ${key} -> ${response}`) - } + this.logMsgIfDebug(() => console.debug(`Successfully computed flag locally: ${key} -> ${response}`)) } catch (e) { if (e instanceof InconclusiveMatchError) { - if (this.debugMode) { - console.debug(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`) - } + this.logMsgIfDebug(() => console.debug(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`)) } else if (e instanceof Error) { this.onError?.(new Error(`Error computing flag locally: ${key}: ${e}`)) } @@ -219,18 +221,18 @@ class FeatureFlagsPoller { const groupName = this.groupTypeMapping[String(aggregation_group_type_index)] if (!groupName) { - if (this.debugMode) { + this.logMsgIfDebug(() => console.warn( `[FEATURE FLAGS] Unknown group type index ${aggregation_group_type_index} for feature flag ${flag.key}` ) - } + ) throw new InconclusiveMatchError('Flag has unknown group type index') } if (!(groupName in groups)) { - if (this.debugMode) { + this.logMsgIfDebug(() => console.warn(`[FEATURE FLAGS] Can't compute group feature flag: ${flag.key} without group names passed in`) - } + ) return false } @@ -307,9 +309,7 @@ class FeatureFlagsPoller { ): boolean { const rolloutPercentage = condition.rollout_percentage const warnFunction = (msg: string): void => { - if (this.debugMode) { - console.warn(msg) - } + this.logMsgIfDebug(() => console.warn(msg)) } if ((condition.properties || []).length > 0) { for (const prop of condition.properties) { diff --git a/posthog-react-native/CHANGELOG.md b/posthog-react-native/CHANGELOG.md index 64cf9098..384deeb5 100644 --- a/posthog-react-native/CHANGELOG.md +++ b/posthog-react-native/CHANGELOG.md @@ -1,5 +1,9 @@ # Next +# 3.3.4 - 2024-10-14 + +1. fix: only log messages if debug is enabled + # 3.3.3 - 2024-10-11 1. fix: bootstrap flags do not overwrite the current values diff --git a/posthog-react-native/package.json b/posthog-react-native/package.json index 462a8db0..763c2aad 100644 --- a/posthog-react-native/package.json +++ b/posthog-react-native/package.json @@ -1,6 +1,6 @@ { "name": "posthog-react-native", - "version": "3.3.3", + "version": "3.3.4", "main": "lib/posthog-react-native/index.js", "files": [ "lib/" diff --git a/posthog-react-native/src/posthog-rn.ts b/posthog-react-native/src/posthog-rn.ts index 0fd7f9e6..fc28d2fb 100644 --- a/posthog-react-native/src/posthog-rn.ts +++ b/posthog-react-native/src/posthog-rn.ts @@ -130,8 +130,10 @@ export class PostHog extends PostHogCore { if (options?.captureNativeAppLifecycleEvents) { if (this._persistence === 'memory') { - console.warn( - 'PostHog was initialised with persistence set to "memory", capturing native app events is not supported.' + this.logMsgIfDebug(() => + console.warn( + 'PostHog was initialised with persistence set to "memory", capturing native app events is not supported.' + ) ) } else { void this.captureNativeAppLifecycleEvents() @@ -223,9 +225,11 @@ export class PostHog extends PostHogCore { try { OptionalReactNativeSessionReplay.endSession() OptionalReactNativeSessionReplay.startSession(sessionId) - console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`) + this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`)) } catch (e) { - console.error('PostHog Debug', `Session replay failed to start with sessionId: ${e}.`) + this.logMsgIfDebug(() => + console.error('PostHog Debug', `Session replay failed to start with sessionId: ${e}.`) + ) } } this._currentSessionId = sessionId @@ -239,9 +243,9 @@ export class PostHog extends PostHogCore { if (this._enableSessionReplay && OptionalReactNativeSessionReplay) { try { OptionalReactNativeSessionReplay.endSession() - console.info('PostHog Debug', `Session replay ended.`) + this.logMsgIfDebug(() => console.info('PostHog Debug', `Session replay ended.`)) } catch (e) { - console.error('PostHog Debug', `Session replay failed to end: ${e}.`) + this.logMsgIfDebug(() => console.error('PostHog Debug', `Session replay failed to end: ${e}.`)) } } } @@ -254,9 +258,11 @@ export class PostHog extends PostHogCore { try { distinctId = distinctId || previousDistinctId OptionalReactNativeSessionReplay.identify(distinctId, this.getAnonymousId()) - console.info('PostHog Debug', `Session replay identified with distinctId ${distinctId}.`) + this.logMsgIfDebug(() => + console.info('PostHog Debug', `Session replay identified with distinctId ${distinctId}.`) + ) } catch (e) { - console.error('PostHog Debug', `Session replay failed to identify: ${e}.`) + this.logMsgIfDebug(() => console.error('PostHog Debug', `Session replay failed to identify: ${e}.`)) } } } @@ -268,7 +274,7 @@ export class PostHog extends PostHogCore { private async startSessionReplay(options?: PostHogOptions): Promise { this._enableSessionReplay = options?.enableSessionReplay if (!this._enableSessionReplay) { - console.info('PostHog Debug', 'Session replay is not enabled.') + this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay is not enabled.')) return } @@ -281,7 +287,9 @@ export class PostHog extends PostHogCore { androidDebouncerDelayMs: 500, } - console.log('PostHog Debug', `Session replay sdk config: ${JSON.stringify(sdkReplayConfig)}`) + this.logMsgIfDebug(() => + console.log('PostHog Debug', `Session replay sdk config: ${JSON.stringify(sdkReplayConfig)}`) + ) // if Decide has not returned yet, we will start session replay with default config. const sessionReplay = this.getPersistedProperty(PostHogPersistedProperty.SessionReplay) ?? {} @@ -289,13 +297,15 @@ export class PostHog extends PostHogCore { // sessionReplay is always an object, if its a boolean, its false if disabled if (sessionReplay) { const decideReplayConfig = (sessionReplay as { [key: string]: JsonType }) ?? {} - console.log('PostHog Debug', `Session replay decide cached config: ${JSON.stringify(decideReplayConfig)}`) + this.logMsgIfDebug(() => + console.log('PostHog Debug', `Session replay decide cached config: ${JSON.stringify(decideReplayConfig)}`) + ) if (OptionalReactNativeSessionReplay) { const sessionId = this.getSessionId() if (sessionId.length === 0) { - console.warn('PostHog Debug', 'Session replay enabled but no sessionId found.') + this.logMsgIfDebug(() => console.warn('PostHog Debug', 'Session replay enabled but no sessionId found.')) return } @@ -308,23 +318,27 @@ export class PostHog extends PostHogCore { sdkVersion: this.getLibraryVersion(), } - console.log('PostHog Debug', `Session replay sdk options: ${JSON.stringify(sdkOptions)}`) + this.logMsgIfDebug(() => + console.log('PostHog Debug', `Session replay sdk options: ${JSON.stringify(sdkOptions)}`) + ) try { if (!(await OptionalReactNativeSessionReplay.isEnabled())) { await OptionalReactNativeSessionReplay.start(sessionId, sdkOptions, sdkReplayConfig, decideReplayConfig) - console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`) + this.logMsgIfDebug(() => + console.info('PostHog Debug', `Session replay started with sessionId ${sessionId}.`) + ) } else { - console.log('PostHog Debug', `Session replay already started.`) + this.logMsgIfDebug(() => console.log('PostHog Debug', `Session replay already started.`)) } } catch (e) { - console.error('PostHog Debug', `Session replay failed to start: ${e}.`) + this.logMsgIfDebug(() => console.error('PostHog Debug', `Session replay failed to start: ${e}.`)) } } else { - console.warn('PostHog Debug', 'Session replay enabled but not installed.') + this.logMsgIfDebug(() => console.warn('PostHog Debug', 'Session replay enabled but not installed.')) } } else { - console.info('PostHog Debug', 'Session replay disabled.') + this.logMsgIfDebug(() => console.info('PostHog Debug', 'Session replay disabled.')) } } @@ -340,10 +354,12 @@ export class PostHog extends PostHogCore { const appVersion = this._appProperties.$app_version if (!appBuild || !appVersion) { - console.warn( - 'PostHog could not track installation/update/open, as the build and version were not set. ' + - 'This can happen if some dependencies are not installed correctly, or if you have provided' + - 'customAppProperties but not included $app_build or $app_version.' + this.logMsgIfDebug(() => + console.warn( + 'PostHog could not track installation/update/open, as the build and version were not set. ' + + 'This can happen if some dependencies are not installed correctly, or if you have provided' + + 'customAppProperties but not included $app_build or $app_version.' + ) ) } if (appBuild) {