Skip to content

Commit 29d0165

Browse files
committed
add loggerWithContext to web
1 parent 56d11f5 commit 29d0165

File tree

9 files changed

+61
-13
lines changed

9 files changed

+61
-13
lines changed

common/src/types/contracts/logger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export type Logger = {
1010
warn: LoggerFn
1111
error: LoggerFn
1212
}
13+
14+
export type LoggerWithContextFn = (context: Record<string, any>) => Logger

web/src/app/api/v1/agent-runs/[runId]/steps/_post.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
1010
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11-
import type { Logger } from '@codebuff/common/types/contracts/logger'
11+
import type {
12+
Logger,
13+
LoggerWithContextFn,
14+
} from '@codebuff/common/types/contracts/logger'
1215
import type { CodebuffPgDatabase } from '@codebuff/internal/db/types'
1316
import type { NextRequest } from 'next/server'
1417

@@ -29,10 +32,19 @@ export async function postAgentRunsSteps(params: {
2932
runId: string
3033
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
3134
logger: Logger
35+
loggerWithContext: LoggerWithContextFn
3236
trackEvent: TrackEventFn
3337
db: CodebuffPgDatabase
3438
}) {
35-
const { req, runId, getUserInfoFromApiKey, logger, trackEvent, db } = params
39+
const {
40+
req,
41+
runId,
42+
getUserInfoFromApiKey,
43+
loggerWithContext,
44+
trackEvent,
45+
db,
46+
} = params
47+
let { logger } = params
3648

3749
const apiKey = extractApiKeyFromHeader(req)
3850

@@ -46,7 +58,7 @@ export async function postAgentRunsSteps(params: {
4658
// Get user info
4759
const userInfo = await getUserInfoFromApiKey({
4860
apiKey,
49-
fields: ['id'],
61+
fields: ['id', 'email', 'discord_id'],
5062
logger,
5163
})
5264

@@ -56,6 +68,7 @@ export async function postAgentRunsSteps(params: {
5668
{ status: 404 },
5769
)
5870
}
71+
logger = loggerWithContext({ userInfo })
5972

6073
// Parse and validate request body
6174
let body: unknown

web/src/app/api/v1/agent-runs/[runId]/steps/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { postAgentRunsSteps } from './_post'
66
import type { NextRequest } from 'next/server'
77

88
import { getUserInfoFromApiKey } from '@/db/user'
9-
import { logger } from '@/util/logger'
9+
import { logger, loggerWithContext } from '@/util/logger'
1010

1111
export async function POST(
1212
req: NextRequest,
@@ -18,6 +18,7 @@ export async function POST(
1818
runId,
1919
getUserInfoFromApiKey,
2020
logger,
21+
loggerWithContext,
2122
trackEvent,
2223
db,
2324
})

web/src/app/api/v1/agent-runs/_post.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
1010
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11-
import type { Logger } from '@codebuff/common/types/contracts/logger'
11+
import type {
12+
Logger,
13+
LoggerWithContextFn,
14+
} from '@codebuff/common/types/contracts/logger'
1215
import type { CodebuffPgDatabase } from '@codebuff/internal/db/types'
1316
import type { NextRequest } from 'next/server'
1417

@@ -169,10 +172,13 @@ export async function postAgentRuns(params: {
169172
req: NextRequest
170173
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
171174
logger: Logger
175+
loggerWithContext: LoggerWithContextFn
172176
trackEvent: TrackEventFn
173177
db: CodebuffPgDatabase
174178
}) {
175-
const { req, getUserInfoFromApiKey, logger, trackEvent, db } = params
179+
const { req, getUserInfoFromApiKey, loggerWithContext, trackEvent, db } =
180+
params
181+
let { logger } = params
176182

177183
const apiKey = extractApiKeyFromHeader(req)
178184

@@ -186,7 +192,7 @@ export async function postAgentRuns(params: {
186192
// Get user info
187193
const userInfo = await getUserInfoFromApiKey({
188194
apiKey,
189-
fields: ['id'],
195+
fields: ['id', 'email', 'discord_id'],
190196
logger,
191197
})
192198

@@ -197,6 +203,8 @@ export async function postAgentRuns(params: {
197203
)
198204
}
199205

206+
logger = loggerWithContext({ userInfo })
207+
200208
// Track API request
201209
trackEvent({
202210
event: AnalyticsEvent.AGENT_RUN_API_REQUEST,

web/src/app/api/v1/agent-runs/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { postAgentRuns } from './_post'
66
import type { NextRequest } from 'next/server'
77

88
import { getUserInfoFromApiKey } from '@/db/user'
9-
import { logger } from '@/util/logger'
9+
import { logger, loggerWithContext } from '@/util/logger'
1010

1111
export async function POST(req: NextRequest) {
12-
return postAgentRuns({ req, getUserInfoFromApiKey, logger, trackEvent, db })
12+
return postAgentRuns({ req, getUserInfoFromApiKey, logger, loggerWithContext, trackEvent, db })
1313
}

web/src/app/api/v1/chat/completions/_post.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type {
1010
GetAgentRunFromIdFn,
1111
GetUserInfoFromApiKeyFn,
1212
} from '@codebuff/common/types/contracts/database'
13-
import type { Logger } from '@codebuff/common/types/contracts/logger'
13+
import type {
14+
Logger,
15+
LoggerWithContextFn,
16+
} from '@codebuff/common/types/contracts/logger'
1417
import type { NextRequest } from 'next/server'
1518

1619
import {
@@ -23,6 +26,7 @@ export async function postChatCompletions(params: {
2326
req: NextRequest
2427
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
2528
logger: Logger
29+
loggerWithContext: LoggerWithContextFn
2630
trackEvent: TrackEventFn
2731
getUserUsageData: GetUserUsageDataFn
2832
getAgentRunFromId: GetAgentRunFromIdFn
@@ -32,13 +36,14 @@ export async function postChatCompletions(params: {
3236
const {
3337
req,
3438
getUserInfoFromApiKey,
35-
logger,
39+
loggerWithContext,
3640
trackEvent,
3741
getUserUsageData,
3842
getAgentRunFromId,
3943
fetch,
4044
insertMessageBigquery,
4145
} = params
46+
let { logger } = params
4247

4348
try {
4449
// Parse request body
@@ -80,7 +85,7 @@ export async function postChatCompletions(params: {
8085
// Get user info
8186
const userInfo = await getUserInfoFromApiKey({
8287
apiKey,
83-
fields: ['id'],
88+
fields: ['id', 'email', 'discord_id'],
8489
logger,
8590
})
8691
if (!userInfo) {
@@ -97,6 +102,7 @@ export async function postChatCompletions(params: {
97102
{ status: 401 },
98103
)
99104
}
105+
logger = loggerWithContext({ userInfo })
100106

101107
const userId = userInfo.id
102108

@@ -107,6 +113,7 @@ export async function postChatCompletions(params: {
107113
properties: {
108114
hasStream: !!bodyStream,
109115
hasRunId: !!runId,
116+
userInfo,
110117
},
111118
logger,
112119
})

web/src/app/api/v1/chat/completions/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import type { NextRequest } from 'next/server'
88

99
import { getAgentRunFromId } from '@/db/agent-run'
1010
import { getUserInfoFromApiKey } from '@/db/user'
11-
import { logger } from '@/util/logger'
11+
import { logger, loggerWithContext } from '@/util/logger'
1212

1313
export async function POST(req: NextRequest) {
1414
return postChatCompletions({
1515
req,
1616
getUserInfoFromApiKey,
1717
logger,
18+
loggerWithContext,
1819
trackEvent,
1920
getUserUsageData,
2021
getAgentRunFromId,

web/src/util/logger.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { env } from '@codebuff/common/env'
66
import { splitData } from '@codebuff/common/util/split-data'
77
import pino from 'pino'
88

9+
import type { LoggerWithContextFn } from '@codebuff/common/types/contracts/logger'
10+
import type { ParamsOf } from '@codebuff/common/types/function-params'
11+
912
// --- Constants ---
1013
const MAX_LENGTH = 65535 // Max total log size is sometimes 100k (sometimes 65535?)
1114
const BUFFER = 1000 // Buffer for context, etc.
@@ -117,3 +120,16 @@ export const logger: Record<LogLevel, pino.LogFn> =
117120
]
118121
}),
119122
) as Record<LogLevel, pino.LogFn>)
123+
124+
export function loggerWithContext(
125+
context: ParamsOf<LoggerWithContextFn>,
126+
): ReturnType<LoggerWithContextFn> {
127+
return {
128+
debug: (data: any, ...args) =>
129+
logger.debug({ ...context, ...data }, ...args),
130+
info: (data: any, ...args) => logger.info({ ...context, ...data }, ...args),
131+
warn: (data: any, ...args) => logger.warn({ ...context, ...data }, ...args),
132+
error: (data: any, ...args) =>
133+
logger.error({ ...context, ...data }, ...args),
134+
}
135+
}

0 commit comments

Comments
 (0)