Skip to content

Commit c7a85aa

Browse files
committed
Cli logs become posthog events. Pass cli logger through sdk
1 parent e3b4eb2 commit c7a85aa

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

cli/src/utils/codebuff-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export async function getCodebuffClient(): Promise<CodebuffClient | null> {
7676
apiKey,
7777
cwd: projectRoot,
7878
agentDefinitions,
79+
logger,
7980
overrideTools: {
8081
ask_user: async (input: ClientToolCall<'ask_user'>['input']) => {
8182
const askUserResponse = await AskUserBridge.request(

cli/src/utils/logger.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { appendFileSync, existsSync, mkdirSync, unlinkSync } from 'fs'
22
import path, { dirname } from 'path'
33
import { format as stringFormat } from 'util'
4+
import { pino } from 'pino'
45

56
import { env, IS_DEV, IS_TEST, IS_CI } from '@codebuff/common/env'
67
import { createAnalyticsDispatcher } from '@codebuff/common/util/analytics-dispatcher'
7-
import { pino } from 'pino'
8+
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
9+
import { getAnalyticsEventId } from '@codebuff/common/util/analytics-log'
810

911
import {
1012
flushAnalytics,
@@ -145,6 +147,18 @@ function sendAnalyticsAndLog(
145147
})
146148
}
147149

150+
// Send all log events to PostHog in production for better observability
151+
// Skip if the log already has an eventId (to avoid duplicate tracking)
152+
const hasEventId = includeData && getAnalyticsEventId(normalizedData) !== null
153+
if (!IS_DEV && !IS_TEST && !IS_CI && !hasEventId) {
154+
trackEvent(AnalyticsEvent.CLI_LOG, {
155+
level,
156+
msg: stringFormat(normalizedMsg ?? '', ...args),
157+
...(includeData ? { data: normalizedData } : {}),
158+
...loggerContext,
159+
})
160+
}
161+
148162
// In dev mode, use appendFileSync for real-time logging (Bun has issues with pino sync)
149163
// In prod mode, use pino for better performance
150164
if (IS_DEV && logPath) {

common/src/constants/analytics-events.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,7 @@ export enum AnalyticsEvent {
125125

126126
// Common
127127
FLUSH_FAILED = 'common.flush_failed',
128+
129+
// Client Logging - for sending logger events to PostHog in production
130+
CLI_LOG = 'cli.log',
128131
}

sdk/src/impl/agent-runtime.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ export function getAgentRuntimeImpl(
8181
trackEvent,
8282

8383
// Other
84-
logger: logger ?? {
85-
info: () => {},
86-
debug: () => {},
87-
warn: () => {},
88-
error: () => {},
89-
},
84+
logger: logger ?? noopLogger,
9085
fetch: globalThis.fetch,
9186

9287
// Client (WebSocket)
@@ -101,3 +96,10 @@ export function getAgentRuntimeImpl(
10196
apiKey,
10297
}
10398
}
99+
100+
const noopLogger: Logger = {
101+
debug: () => {},
102+
info: () => {},
103+
warn: () => {},
104+
error: () => {},
105+
}

0 commit comments

Comments
 (0)