-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
48 lines (40 loc) · 1.44 KB
/
logger.ts
File metadata and controls
48 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { PLUGIN_NAME } from "./constants"
import { appendFileSync, mkdirSync } from "fs"
import { join } from "path"
import { homedir } from "os"
const LOG_FILE = join(homedir(), ".config", "opencode", "opencode-fallback.log")
// Ensure directory exists
try {
mkdirSync(join(homedir(), ".config", "opencode"), { recursive: true })
} catch {
// Directory might already exist
}
function writeToFile(level: string, message: string, context?: Record<string, unknown>): void {
const timestamp = new Date().toISOString()
const contextStr = context ? ` ${JSON.stringify(context)}` : ""
const logLine = `[${timestamp}] [${level}] [${PLUGIN_NAME}] ${message}${contextStr}\n`
try {
appendFileSync(LOG_FILE, logLine)
} catch {
// Silently fail if can't write to file
}
}
// Set to true to enable console logging (for debugging only)
const DEBUG_MODE = false
export function logInfo(message: string, context?: Record<string, unknown>): void {
if (DEBUG_MODE) {
const contextStr = context ? ` ${JSON.stringify(context)}` : ""
console.log(`[${PLUGIN_NAME}] ${message}${contextStr}`)
}
writeToFile("INFO", message, context)
}
export function logError(message: string, context?: Record<string, unknown>): void {
if (DEBUG_MODE) {
const contextStr = context ? ` ${JSON.stringify(context)}` : ""
console.error(`[${PLUGIN_NAME}] ${message}${contextStr}`)
}
writeToFile("ERROR", message, context)
}
export function getLogFilePath(): string {
return LOG_FILE
}