Skip to content

Commit

Permalink
fix: add debug logs only in debug mode (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Feb 27, 2023
1 parent c9ef96d commit 3b9350c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Promise<any>> = {}

private _optoutOverride: boolean | undefined
Expand Down Expand Up @@ -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))
}
Expand Down
3 changes: 3 additions & 0 deletions posthog-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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()`.
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": "2.5.3",
"version": "2.5.4",
"description": "PostHog Node.js integration",
"repository": "PostHog/posthog-node",
"scripts": {
Expand Down
12 changes: 11 additions & 1 deletion posthog-node/src/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FeatureFlagsPoller {
host: FeatureFlagsPollerOptions['host']
poller?: NodeJS.Timeout
fetch: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>
debugMode: boolean = false

constructor({
pollingInterval,
Expand All @@ -76,6 +77,10 @@ class FeatureFlagsPoller {
void this.loadFeatureFlags()
}

debug(enabled: boolean = true): void {
this.debugMode = enabled
}

async getFeatureFlag(
key: string,
distinctId: string,
Expand All @@ -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}`)
}
Expand Down
5 changes: 5 additions & 0 deletions posthog-node/src/posthog-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit 3b9350c

Please sign in to comment.