Skip to content

Commit 73fc046

Browse files
androidandclaude
andcommitted
fix(log): route fork logger shim to file, not stdout/stderr
The legacy Log shim wrote INFO/DEBUG to process.stdout and WARN/ERROR to process.stderr. In the TUI stdout IS the screen, so log lines from local-provider-sync, openai-compatible discovery, and beads were painted into the terminal — landing inside the chat input and making a fresh launch look broken. Write to opencode's log file (Global.Path.log/opencode.log) like the Effect file logger, mirroring to stderr only when OPENCODE_PRINT_LOGS=1. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 9d4ed82 commit 73fc046

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎packages/core/src/util/log.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
// To retire this shim later, migrate those callers to upstream's Effect logging
1212
// and delete this file (and its manifest entry).
1313

14+
import fs from "fs"
15+
import path from "path"
16+
import { Global } from "../global"
17+
1418
const LEVELS = { DEBUG: 10, INFO: 20, WARN: 30, ERROR: 40 } as const
1519
type LevelName = keyof typeof LEVELS
1620

21+
const LOG_FILE = path.join(Global.Path.log, "opencode.log")
22+
let logDirReady = false
23+
1724
function threshold(): number {
1825
const env = (process.env["OPENCODE_LOG_LEVEL"] ?? "INFO").toUpperCase()
1926
return LEVELS[env as LevelName] ?? LEVELS.INFO
@@ -47,8 +54,21 @@ export function create(tags: Record<string, any> = {}): Logger {
4754
const emit = (level: LevelName, message?: any, extra?: Record<string, any>) => {
4855
if (LEVELS[level] < threshold()) return
4956
const line = format(level, message, extra)
50-
if (level === "ERROR" || level === "WARN") process.stderr.write(line + "\n")
51-
else process.stdout.write(line + "\n")
57+
// fork: write to opencode's log file, never stdout/stderr. Writing to the
58+
// terminal corrupts the TUI — these lines (local-provider-sync,
59+
// openai-compatible discovery, beads) were rendering inside the chat input.
60+
// Mirror to stderr only when OPENCODE_PRINT_LOGS=1, matching the Effect
61+
// file logger in observability/logging.ts.
62+
try {
63+
if (!logDirReady) {
64+
fs.mkdirSync(Global.Path.log, { recursive: true })
65+
logDirReady = true
66+
}
67+
fs.appendFileSync(LOG_FILE, line + "\n")
68+
} catch {
69+
// disk/log unavailable — drop the line rather than crash or corrupt the UI.
70+
}
71+
if (process.env["OPENCODE_PRINT_LOGS"] === "1") process.stderr.write(line + "\n")
5272
}
5373

5474
const result: Logger = {

0 commit comments

Comments
 (0)