feat: use ctx.app.log() for OpenCode logging with file fallback

Плагин теперь пытается использовать OpenCode API логирования
(ctx.app.log) для записи в ~/.local/share/opencode/log/.
При ошибке — fallback на файловый лог ~/.opencode-matrix-plugin/plugin.log.
This commit is contained in:
Бородин Роман 2026-09-26 01:00:21 +03:00
parent eabd9dbe9b
commit c95ac899d2
2 changed files with 58 additions and 11 deletions

View File

@ -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, logError } from "./logger.js" import { setAppLog, 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
@ -296,6 +296,11 @@ async function v2Setup(ctx: any) {
: 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)
// Try to use OpenCode's logging API
if (ctx?.app?.log) {
setAppLog(ctx.app.log.bind(ctx.app))
}
try { try {
const { options } = await loadConfig(ctx.options) const { options } = await loadConfig(ctx.options)
return await runPlugin(ctx, options, dataDir) return await runPlugin(ctx, options, dataDir)

View File

@ -5,20 +5,62 @@ import path from "path"
const LOG_DIR = path.join(os.homedir(), ".opencode-matrix-plugin") const LOG_DIR = path.join(os.homedir(), ".opencode-matrix-plugin")
const LOG_FILE = path.join(LOG_DIR, "plugin.log") const LOG_FILE = path.join(LOG_DIR, "plugin.log")
let appLog: ((data: any) => Promise<void>) | null = null
export function setAppLog(fn: (data: any) => Promise<void>): void {
appLog = fn
}
export function log(message: string): void { export function log(message: string): void {
try { // Try OpenCode's logging API first
fs.mkdirSync(LOG_DIR, { recursive: true }) if (appLog) {
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`) appLog({
} catch { service: "matrix-plugin",
// silent level: "info",
message,
}).catch(() => {
// Fallback to file logging
try {
fs.mkdirSync(LOG_DIR, { recursive: true })
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
} catch {
// silent
}
})
} else {
// No app log available, use file logging
try {
fs.mkdirSync(LOG_DIR, { recursive: true })
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
} catch {
// silent
}
} }
} }
export function logError(message: string): void { export function logError(message: string): void {
try { // Try OpenCode's logging API first
fs.mkdirSync(LOG_DIR, { recursive: true }) if (appLog) {
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`) appLog({
} catch { service: "matrix-plugin",
// silent level: "error",
message,
}).catch(() => {
// Fallback to file logging
try {
fs.mkdirSync(LOG_DIR, { recursive: true })
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
} catch {
// silent
}
})
} else {
// No app log available, use file logging
try {
fs.mkdirSync(LOG_DIR, { recursive: true })
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
} catch {
// silent
}
} }
} }