Skip to content

Commit 6e5652a

Browse files
committed
feat: Log request and response headers in logger
1 parent 1b85112 commit 6e5652a

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/lib/logger.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const logger = {
4444
endpoint: string,
4545
method: string,
4646
payload: unknown,
47+
headers?: Record<string, string>,
4748
): Promise<void> {
4849
if (!this.options.enabled || !this.options.filePath) return
4950

@@ -52,6 +53,9 @@ export const logger = {
5253
`## Request - ${timestamp}`,
5354
`Endpoint: ${endpoint}`,
5455
`Method: ${method}`,
56+
headers ?
57+
`Headers:\n\`\`\`json\n${JSON.stringify(headers, null, 2)}\n\`\`\``
58+
: "",
5559
`Payload:`,
5660
`\`\`\`json`,
5761
JSON.stringify(payload, null, 2),
@@ -66,13 +70,20 @@ export const logger = {
6670
}
6771
},
6872

69-
async logResponse(endpoint: string, response: unknown): Promise<void> {
73+
async logResponse(
74+
endpoint: string,
75+
response: unknown,
76+
headers?: Record<string, string>,
77+
): Promise<void> {
7078
if (!this.options.enabled || !this.options.filePath) return
7179

7280
const timestamp = new Date().toISOString()
7381
const logEntry = [
7482
`## Response - ${timestamp}`,
7583
`Endpoint: ${endpoint}`,
84+
headers ?
85+
`Headers:\n\`\`\`json\n${JSON.stringify(headers, null, 2)}\n\`\`\``
86+
: "",
7687
`Response:`,
7788
`\`\`\`json`,
7889
JSON.stringify(response, null, 2),

src/routes/chat-completions/handler.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ function createCondensedStreamingResponse(
3838
export async function handlerStreaming(c: Context) {
3939
const payload = await c.req.json<ChatCompletionsPayload>()
4040

41+
// Convert request headers to a regular object from Headers
42+
const requestHeaders = c.req.header()
43+
4144
// Log the request at the beginning for both streaming and non-streaming cases
42-
await logger.logRequest("/chat/completions", "POST", payload)
45+
await logger.logRequest("/chat/completions", "POST", payload, requestHeaders)
4346

4447
if (payload.stream) {
4548
const response = await chatCompletionsStream(payload)
@@ -52,7 +55,7 @@ export async function handlerStreaming(c: Context) {
5255
for await (const chunk of response) {
5356
await stream.writeSSE(chunk as SSEMessage)
5457

55-
if (!logger.options.enabled) continue // Changed from return to continue
58+
if (!logger.options.enabled) continue
5659

5760
// Check if chunk data is "DONE" or not a valid JSON string
5861
if (!chunk.data || chunk.data === "[DONE]") {
@@ -83,15 +86,18 @@ export async function handlerStreaming(c: Context) {
8386
collectedContent,
8487
)
8588

86-
await logger.logResponse("/chat/completions", condensedResponse)
89+
await logger.logResponse("/chat/completions", condensedResponse, {})
8790
}
8891
})
8992
}
9093

9194
const response = await chatCompletions(payload)
9295

93-
// Log the non-streaming response
94-
await logger.logResponse("/chat/completions", response)
96+
// Get response headers if any
97+
const responseHeaders = {} // Empty placeholder for response headers
98+
99+
// Log the non-streaming response with headers
100+
await logger.logResponse("/chat/completions", response, responseHeaders)
95101

96102
return c.json(response)
97103
}

0 commit comments

Comments
 (0)