v_1.7
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { LOG_FILE } from './config.js';
|
||||
|
||||
// Simple JSON-lines log stored on the data volume. Newest entries are returned
|
||||
// first for the management page.
|
||||
export async function logEvent({ level = 'info', actor = 'system', action = 'event', message = '', meta } = {}) {
|
||||
const entry = { ts: Date.now(), level, actor, action, message, ...(meta ? { meta } : {}) };
|
||||
try {
|
||||
await fs.mkdir(path.dirname(LOG_FILE), { recursive: true });
|
||||
await fs.appendFile(LOG_FILE, JSON.stringify(entry) + '\n');
|
||||
} catch {
|
||||
// Logging must never take the app down.
|
||||
}
|
||||
const tag = level === 'error' ? 'ERROR' : level === 'warn' ? 'warn' : 'log';
|
||||
console.log(`[${tag}] ${action}: ${message}`);
|
||||
return entry;
|
||||
}
|
||||
|
||||
export async function readLogs(limit = 300) {
|
||||
try {
|
||||
const raw = await fs.readFile(LOG_FILE, 'utf8');
|
||||
const lines = raw.split('\n').filter(Boolean);
|
||||
return lines.slice(-limit).reverse().map((l) => {
|
||||
try { return JSON.parse(l); } catch { return { ts: 0, level: 'info', message: l }; }
|
||||
});
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function clearLogs() {
|
||||
try { await fs.writeFile(LOG_FILE, ''); } catch {}
|
||||
}
|
||||
Reference in New Issue
Block a user