fix: call setLogDir() before loadConfig() so all logs go to storagePath

loadConfig() вызывал log() до setLogDir() — писался в /tmp.
Теперь setLogDir() вызывается в v2Setup() до loadConfig().
This commit is contained in:
Бородин Роман 2026-09-26 00:30:23 +03:00
parent e30bdd50e7
commit b5bc06e722
2 changed files with 11 additions and 20 deletions

View File

@ -10,11 +10,6 @@ import { setLogDir, log } from "./logger.js"
// Plugin data directory — uses the same default as matrix-client // Plugin data directory — uses the same default as matrix-client
// ============================================================================= // =============================================================================
function getPluginDataDir(options: any): string {
const storagePath = options.storagePath || path.join(os.homedir(), ".local", "share", "opencode-matrix-bot")
return path.join(storagePath, "plugin-data")
}
function ensureDataDir(dir: string): void { function ensureDataDir(dir: string): void {
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true }) fs.mkdirSync(dir, { recursive: true })
@ -65,13 +60,11 @@ function releaseLock(): void {
// Plugin Setup // Plugin Setup
// ============================================================================= // =============================================================================
async function runPlugin(ctx: any, options: any) { async function runPlugin(ctx: any, options: any, dataDir: string) {
const dataDir = getPluginDataDir(options)
ensureDataDir(dataDir) ensureDataDir(dataDir)
LOCK_FILE = `${dataDir}/lock` LOCK_FILE = `${dataDir}/lock`
const SETUP_STARTED_LOG = `${dataDir}/setup-started.log` const SETUP_STARTED_LOG = `${dataDir}/setup-started.log`
const SETUP_ERROR_LOG = `${dataDir}/setup-error.log` const SETUP_ERROR_LOG = `${dataDir}/setup-error.log`
setLogDir(dataDir)
log("Plugin module loaded") log("Plugin module loaded")
// Acquire global lock — only one instance (server or client) starts the bot // Acquire global lock — only one instance (server or client) starts the bot
@ -300,15 +293,17 @@ async function runPlugin(ctx: any, options: any) {
// ============================================================================= // =============================================================================
async function v2Setup(ctx: any) { async function v2Setup(ctx: any) {
try {
const { options } = await loadConfig(ctx.options)
return await runPlugin(ctx, options)
} catch (err) {
const rawStorage = (ctx.options as any)?.storagePath const rawStorage = (ctx.options as any)?.storagePath
const dataDir = rawStorage const dataDir = rawStorage
? path.join(rawStorage, "plugin-data") ? path.join(rawStorage, "plugin-data")
: path.join(os.homedir(), ".local", "share", "opencode-matrix-bot", "plugin-data") : path.join(os.homedir(), ".local", "share", "opencode-matrix-bot", "plugin-data")
ensureDataDir(dataDir) ensureDataDir(dataDir)
setLogDir(dataDir)
try {
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`) fs.appendFileSync(`${dataDir}/setup-error.log`, `[${new Date().toISOString()}] ${String(err)}\n`)
log(`V2 setup error: ${String(err)}`) log(`V2 setup error: ${String(err)}`)
return () => {} return () => {}

View File

@ -1,5 +1,4 @@
import fs from "fs" import fs from "fs"
import os from "os"
import path from "path" import path from "path"
let LOG_DIR = "/tmp/opencode-matrix-plugin" let LOG_DIR = "/tmp/opencode-matrix-plugin"
@ -12,14 +11,11 @@ export function setLogDir(dir: string): void {
export function log(message: string): void { export function log(message: string): void {
try { try {
const timestamp = new Date().toISOString()
const line = `[${timestamp}] ${message}\n`
fs.mkdirSync(LOG_DIR, { recursive: true }) fs.mkdirSync(LOG_DIR, { recursive: true })
fs.appendFileSync(LOG_FILE, line) fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${message}\n`)
} catch (e) { } catch (e) {
try { try {
const timestamp = new Date().toISOString() fs.appendFileSync("/tmp/opencode-matrix-fallback.log", `[${new Date().toISOString()}] ${message}\n`)
fs.appendFileSync("/tmp/opencode-matrix-fallback.log", `[${timestamp}] ${message}\n`)
} catch { } catch {
// silent fallback // silent fallback
} }