Skip to content

Commit bf98f99

Browse files
committed
feat: Enhance logging functionality and update CLI description
1 parent da49444 commit bf98f99

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/lib/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const args = {
2121
logs: {
2222
type: "boolean",
2323
default: false,
24-
description: "Write logs to the app directory",
24+
description:
25+
"Write logs to the app directory. Only works with emulate-streaming",
2526
},
2627
} satisfies ArgsDef
2728

src/lib/logger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import fs from "node:fs"
1+
import fs from "node:fs/promises"
22

33
import { CONFIG } from "~/config/config"
44
import { PATHS } from "~/config/paths"
55

66
export function initializeLogger() {
77
if (!CONFIG.LOGGING_ENABLED) return
88

9-
fs.mkdirSync(PATHS.LOG_PATH, { recursive: true })
9+
return fs.mkdir(PATHS.LOG_PATH, { recursive: true })
1010
}
1111

12-
export function logToFile(type: string, message: string) {
12+
export async function logToFile(type: string, message: string) {
1313
if (!CONFIG.LOGGING_ENABLED) return
1414

1515
const timestamp = new Date().toISOString()
16-
fs.appendFileSync(PATHS.LOG_FILE, `${timestamp} ${type}: ${message}\n`)
16+
await fs.appendFile(PATHS.LOG_FILE, `${timestamp} ${type}: ${message}\n`)
1717
}

src/routes/chat-completions/handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { streamSSE } from "hono/streaming"
55

66
import type { ChatCompletionsPayload } from "~/services/copilot/chat-completions/types"
77

8+
import { logToFile } from "~/lib/logger"
89
import { chatCompletions } from "~/services/copilot/chat-completions/service"
910

1011
import { createContentChunk, createFinalChunk, segmentResponse } from "./utils"
@@ -22,8 +23,10 @@ export async function handler(c: Context) {
2223
}))
2324

2425
consola.info("Received request:", loggedPayload)
26+
await logToFile("REQUEST", JSON.stringify(payload, null, 2))
2527

2628
const response = await chatCompletions(payload)
29+
await logToFile("RESPONSE", JSON.stringify(response, null, 2))
2730

2831
if (payload.stream) {
2932
consola.info(`Response from Copilot: ${JSON.stringify(response)}`)

0 commit comments

Comments
 (0)