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:
parent
eabd9dbe9b
commit
c95ac899d2
7
index.ts
7
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, 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)
|
||||||
|
|
|
||||||
42
logger.ts
42
logger.ts
|
|
@ -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 OpenCode's logging API first
|
||||||
|
if (appLog) {
|
||||||
|
appLog({
|
||||||
|
service: "matrix-plugin",
|
||||||
|
level: "info",
|
||||||
|
message,
|
||||||
|
}).catch(() => {
|
||||||
|
// Fallback to file logging
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(LOG_DIR, { recursive: true })
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
||||||
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ${message}\n`)
|
||||||
} catch {
|
} catch {
|
||||||
// silent
|
// 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 OpenCode's logging API first
|
||||||
|
if (appLog) {
|
||||||
|
appLog({
|
||||||
|
service: "matrix-plugin",
|
||||||
|
level: "error",
|
||||||
|
message,
|
||||||
|
}).catch(() => {
|
||||||
|
// Fallback to file logging
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(LOG_DIR, { recursive: true })
|
fs.mkdirSync(LOG_DIR, { recursive: true })
|
||||||
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
|
fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [matrix-plugin] ERROR: ${message}\n`)
|
||||||
} catch {
|
} catch {
|
||||||
// silent
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue