Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only logging if debug enabled #284

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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))
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions posthog-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion posthog-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-node",
"version": "4.2.0",
"version": "4.2.1",
"description": "PostHog Node.js integration",
"repository": {
"type": "git",
Expand Down
28 changes: 14 additions & 14 deletions posthog-node/src/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class FeatureFlagsPoller {
this.debugMode = enabled
}

private logMsgIfDebug(fn: () => void): void {
if (this.debugMode) {
fn()
}
}

async getFeatureFlag(
key: string,
distinctId: string,
Expand All @@ -99,7 +105,7 @@ class FeatureFlagsPoller {
): Promise<string | boolean | undefined> {
await this.loadFeatureFlags()

let response = undefined
let response: string | boolean | undefined = undefined
let featureFlag = undefined

if (!this.loadedSuccessfullyOnce) {
Expand All @@ -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}`))
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
60 changes: 38 additions & 22 deletions posthog-react-native/src/posthog-rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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}.`))
}
}
}
Expand All @@ -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}.`))
}
}
}
Expand All @@ -268,7 +274,7 @@ export class PostHog extends PostHogCore {
private async startSessionReplay(options?: PostHogOptions): Promise<void> {
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
}

Expand All @@ -281,21 +287,25 @@ 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) ?? {}

// 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
}

Expand All @@ -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.'))
}
}

Expand All @@ -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) {
Expand Down