matrix-plugin/dist/logger.js

68 lines
1.9 KiB
JavaScript

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");
let appLog = null;
export function setAppLog(fn) {
appLog = fn;
}
export function log(message) {
// Try OpenCode's logging API first
if (appLog) {
appLog({
service: "matrix-plugin",
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) {
// Try OpenCode's logging API first
if (appLog) {
appLog({
service: "matrix-plugin",
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
}
}
}