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 { MatrixBotClient } from "./matrix-client.js"
|
||||||
import { SessionManager } from "./session-manager.js"
|
import { SessionManager } from "./session-manager.js"
|
||||||
import { loadConfig } from "./config-loader.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
|
// 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) {
|
async function runPlugin(ctx: any, options: any, dataDir: string) {
|
||||||
ensureDataDir(dataDir)
|
ensureDataDir(dataDir)
|
||||||
LOCK_FILE = `${dataDir}/lock`
|
LOCK_FILE = `${dataDir}/lock`
|
||||||
const SETUP_STARTED_LOG = `${dataDir}/setup-started.log`
|
|
||||||
const SETUP_ERROR_LOG = `${dataDir}/setup-error.log`
|
const directory = ctx?.location?.directory || process.cwd()
|
||||||
log("Plugin module loaded")
|
log(`Plugin setup started, directory=${directory}`)
|
||||||
|
|
||||||
// Acquire global lock — only one instance (server or client) starts the bot
|
// Acquire global lock — only one instance (server or client) starts the bot
|
||||||
const locked = tryAcquireLock()
|
const locked = tryAcquireLock()
|
||||||
|
|
@ -78,9 +78,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
||||||
let cleanup: (() => void) | undefined
|
let cleanup: (() => void) | undefined
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const directory = ctx?.location?.directory || process.cwd()
|
log(`Configuration loaded`)
|
||||||
fs.appendFileSync(SETUP_STARTED_LOG, `[${new Date().toISOString()}] setup() called, dir=${directory}\n`)
|
|
||||||
log(`Plugin setup started, directory=${directory}`)
|
|
||||||
|
|
||||||
if (!options.homeserver) {
|
if (!options.homeserver) {
|
||||||
log("Plugin disabled (no homeserver configured)")
|
log("Plugin disabled (no homeserver configured)")
|
||||||
|
|
@ -281,8 +279,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
||||||
|
|
||||||
return cleanup
|
return cleanup
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
fs.appendFileSync(SETUP_ERROR_LOG, `[${new Date().toISOString()}] ${String(err)}\n`)
|
logError(`Setup error: ${String(err)}`)
|
||||||
log(`Setup error: ${String(err)}`)
|
|
||||||
releaseLock()
|
releaseLock()
|
||||||
return () => {}
|
return () => {}
|
||||||
}
|
}
|
||||||
|
|
@ -303,8 +300,7 @@ async function v2Setup(ctx: any) {
|
||||||
const { options } = await loadConfig(ctx.options)
|
const { options } = await loadConfig(ctx.options)
|
||||||
return await runPlugin(ctx, options, dataDir)
|
return await runPlugin(ctx, options, dataDir)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
fs.appendFileSync(`${dataDir}/setup-error.log`, `[${new Date().toISOString()}] ${String(err)}\n`)
|
logError(`V2 setup error: ${String(err)}`)
|
||||||
log(`V2 setup error: ${String(err)}`)
|
|
||||||
return () => {}
|
return () => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
22
logger.ts
22
logger.ts
|
|
@ -1,19 +1,9 @@
|
||||||
import fs from "fs"
|
const PREFIX = "[matrix-plugin]"
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
export function log(message: string): void {
|
export function log(message: string): void {
|
||||||
try {
|
console.log(`${PREFIX} ${message}`)
|
||||||
fs.mkdirSync(LOG_DIR, { recursive: true })
|
}
|
||||||
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${message}\n`)
|
|
||||||
} catch {
|
export function logError(message: string): void {
|
||||||
// silent
|
console.error(`${PREFIX} ${message}`)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue