From 144410049518a06cab612394e99eef219bbaa133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Sat, 26 Sep 2026 00:47:20 +0300 Subject: [PATCH] refactor: replace file logging with console.log (OpenCode V2 native) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - logger.ts теперь использует console.log/console.error с префиксом [matrix-plugin] - Удалена запись в ~/.opencode-matrix-plugin/plugin.log - Удалены setup-started.log и setup-error.log - Ошибки пишутся через logError() в stderr --- index.ts | 18 +++++++----------- logger.ts | 22 ++++++---------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/index.ts b/index.ts index a24c348..6bef488 100644 --- a/index.ts +++ b/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 () => {} } } diff --git a/logger.ts b/logger.ts index 9a72c27..af135f7 100644 --- a/logger.ts +++ b/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}`) }