From 3b9350c6eae1d2aa6b7aae8a3697120ac6f0c4ff Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Mon, 27 Feb 2023 16:28:46 +0000 Subject: [PATCH] fix: add debug logs only in debug mode (#84) --- posthog-core/src/index.ts | 3 +++ posthog-node/CHANGELOG.md | 3 +++ posthog-node/package.json | 2 +- posthog-node/src/feature-flags.ts | 12 +++++++++++- posthog-node/src/posthog-node.ts | 5 +++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/posthog-core/src/index.ts b/posthog-core/src/index.ts index 0d67fde8..ae0cf6de 100644 --- a/posthog-core/src/index.ts +++ b/posthog-core/src/index.ts @@ -33,6 +33,7 @@ export abstract class PostHogCoreStateless { private requestTimeout: number private captureMode: 'form' | 'json' private removeDebugCallback?: () => void + private debugMode: boolean = false private pendingPromises: Record> = {} private _optoutOverride: boolean | undefined @@ -97,6 +98,8 @@ export abstract class PostHogCoreStateless { debug(enabled: boolean = true): void { this.removeDebugCallback?.() + this.debugMode = enabled + if (enabled) { this.removeDebugCallback = this.on('*', (event, payload) => console.log('PostHog Debug', event, payload)) } diff --git a/posthog-node/CHANGELOG.md b/posthog-node/CHANGELOG.md index 5e54967e..d25663d3 100644 --- a/posthog-node/CHANGELOG.md +++ b/posthog-node/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.5.4 - 2023-02-27 + +1. Fix error log for local evaluation of feature flags (InconclusiveMatchError(s)) to only show during debug mode. # 2.5.3 - 2023-02-21 1. Allow passing in a distinctId to `groupIdentify()`. diff --git a/posthog-node/package.json b/posthog-node/package.json index a32073e1..b0d7c3ec 100644 --- a/posthog-node/package.json +++ b/posthog-node/package.json @@ -1,6 +1,6 @@ { "name": "posthog-node", - "version": "2.5.3", + "version": "2.5.4", "description": "PostHog Node.js integration", "repository": "PostHog/posthog-node", "scripts": { diff --git a/posthog-node/src/feature-flags.ts b/posthog-node/src/feature-flags.ts index b556c4a7..da36c8bd 100644 --- a/posthog-node/src/feature-flags.ts +++ b/posthog-node/src/feature-flags.ts @@ -51,6 +51,7 @@ class FeatureFlagsPoller { host: FeatureFlagsPollerOptions['host'] poller?: NodeJS.Timeout fetch: (url: string, options: PostHogFetchOptions) => Promise + debugMode: boolean = false constructor({ pollingInterval, @@ -76,6 +77,10 @@ class FeatureFlagsPoller { void this.loadFeatureFlags() } + debug(enabled: boolean = true): void { + this.debugMode = enabled + } + async getFeatureFlag( key: string, distinctId: string, @@ -102,9 +107,14 @@ 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}`) + } } catch (e) { if (e instanceof InconclusiveMatchError) { - console.error(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`) + if (this.debugMode) { + console.debug(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`) + } } else if (e instanceof Error) { console.error(`Error computing flag locally: ${key}: ${e}`) } diff --git a/posthog-node/src/posthog-node.ts b/posthog-node/src/posthog-node.ts index 57f8315d..7e108c78 100644 --- a/posthog-node/src/posthog-node.ts +++ b/posthog-node/src/posthog-node.ts @@ -92,6 +92,11 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 { return super.optOut() } + debug(enabled: boolean = true): void { + super.debug(enabled) + this.featureFlagsPoller?.debug(enabled) + } + capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp }: EventMessageV1): void { const _capture = (props: EventMessageV1['properties']): void => { super.captureStateless(distinctId, event, props, { timestamp })