refactor: replace file logging with console.log (OpenCode V2 native)
- logger.ts теперь использует console.log/console.error с префиксом [matrix-plugin] - Удалена запись в ~/.opencode-matrix-plugin/plugin.log - Удалены setup-started.log и setup-error.log - Ошибки пишутся через logError() в stderr
This commit is contained in:
parent
79e785330f
commit
1444100495
18
index.ts
18
index.ts
|
|
@ -4,7 +4,7 @@ import path from "path"
|
|||
import { MatrixBotClient } from "./matrix-client.js"
|
||||
import { SessionManager } from "./session-manager.js"
|
||||
import { loadConfig } from "./config-loader.js"
|
||||
import { log } from "./logger.js"
|
||||
import { log, logError } from "./logger.js"
|
||||
|
||||
// =============================================================================
|
||||
// Plugin data directory — uses the same default as matrix-client
|
||||
|
|
@ -63,9 +63,9 @@ function releaseLock(): void {
|
|||
async function runPlugin(ctx: any, options: any, dataDir: string) {
|
||||
ensureDataDir(dataDir)
|
||||
LOCK_FILE = `${dataDir}/lock`
|
||||
const SETUP_STARTED_LOG = `${dataDir}/setup-started.log`
|
||||
const SETUP_ERROR_LOG = `${dataDir}/setup-error.log`
|
||||
log("Plugin module loaded")
|
||||
|
||||
const directory = ctx?.location?.directory || process.cwd()
|
||||
log(`Plugin setup started, directory=${directory}`)
|
||||
|
||||
// Acquire global lock — only one instance (server or client) starts the bot
|
||||
const locked = tryAcquireLock()
|
||||
|
|
@ -78,9 +78,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
|||
let cleanup: (() => void) | undefined
|
||||
|
||||
try {
|
||||
const directory = ctx?.location?.directory || process.cwd()
|
||||
fs.appendFileSync(SETUP_STARTED_LOG, `[${new Date().toISOString()}] setup() called, dir=${directory}\n`)
|
||||
log(`Plugin setup started, directory=${directory}`)
|
||||
log(`Configuration loaded`)
|
||||
|
||||
if (!options.homeserver) {
|
||||
log("Plugin disabled (no homeserver configured)")
|
||||
|
|
@ -281,8 +279,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
|||
|
||||
return cleanup
|
||||
} catch (err) {
|
||||
fs.appendFileSync(SETUP_ERROR_LOG, `[${new Date().toISOString()}] ${String(err)}\n`)
|
||||
log(`Setup error: ${String(err)}`)
|
||||
logError(`Setup error: ${String(err)}`)
|
||||
releaseLock()
|
||||
return () => {}
|
||||
}
|
||||
|
|
@ -303,8 +300,7 @@ async function v2Setup(ctx: any) {
|
|||
const { options } = await loadConfig(ctx.options)
|
||||
return await runPlugin(ctx, options, dataDir)
|
||||
} catch (err) {
|
||||
fs.appendFileSync(`${dataDir}/setup-error.log`, `[${new Date().toISOString()}] ${String(err)}\n`)
|
||||
log(`V2 setup error: ${String(err)}`)
|
||||
logError(`V2 setup error: ${String(err)}`)
|
||||
return () => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
22
logger.ts
22
logger.ts
|
|
@ -1,19 +1,9 @@
|
|||
import fs from "fs"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
|
||||
const LOG_DIR = path.join(os.homedir(), ".opencode-matrix-plugin")
|
||||
const LOG_FILE = path.join(LOG_DIR, "plugin.log")
|
||||
|
||||
export function setLogDir(dir: string): void {
|
||||
// no-op: LOG_FILE is already set to the correct location
|
||||
}
|
||||
const PREFIX = "[matrix-plugin]"
|
||||
|
||||
export function log(message: string): void {
|
||||
try {
|
||||
fs.mkdirSync(LOG_DIR, { recursive: true })
|
||||
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${message}\n`)
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
console.log(`${PREFIX} ${message}`)
|
||||
}
|
||||
|
||||
export function logError(message: string): void {
|
||||
console.error(`${PREFIX} ${message}`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue