Enable runtime config reload and dynamic endpoint management across transports, and add server-side log streaming + UI. - Transport hot-reload: HttpTransport, HttpPostTransport and WsTransport now maintain endpoint lists and start/stop individual endpoints (servers/clients) dynamically; servers/clients use keys/Maps to identify instances; added reloadConfig methods and helper key functions. - OneBot control: OneBotInstance.reloadConfig delegating to transports; OneBotManager.reloadConfig(uin) to load and apply new config; added getPids helper. - Logger & streaming: added in-memory recent log buffer, subscriber API (getRecentLogs, subscribeLogs) and LogEntry shape; web UI exposes /api/logs and /api/logs/stream (SSE) and improved token/query handling. - Web UI: added LogsPage, integrated into layout and sidebar, EventSource client to show live logs; config save now attempts hot-reload and returns reloaded status. - Misc: refactors to use Maps for server lists, cleanup on stop, and various small helpers and safety checks.
145 lines
3.7 KiB
TypeScript
145 lines
3.7 KiB
TypeScript
import { format } from 'util';
|
|
|
|
type LogLevel = 'debug' | 'info' | 'success' | 'warn' | 'error';
|
|
|
|
export interface LogEntry {
|
|
id: number;
|
|
time: string;
|
|
level: LogLevel;
|
|
scope: string;
|
|
message: string;
|
|
line: string;
|
|
}
|
|
|
|
interface LogOptions {
|
|
scope: string;
|
|
}
|
|
|
|
const LEVEL_WEIGHT: Record<LogLevel, number> = {
|
|
debug: 10,
|
|
info: 20,
|
|
success: 25,
|
|
warn: 30,
|
|
error: 40,
|
|
};
|
|
|
|
const LEVEL_LABEL: Record<LogLevel, string> = {
|
|
debug: 'DEBUG',
|
|
info: 'INFO',
|
|
success: 'OK',
|
|
warn: 'WARN',
|
|
error: 'ERROR',
|
|
};
|
|
|
|
const COLOR_CODE: Record<LogLevel, number> = {
|
|
debug: 90,
|
|
info: 36,
|
|
success: 32,
|
|
warn: 33,
|
|
error: 31,
|
|
};
|
|
|
|
const COLOR_SCOPE = 35;
|
|
const COLOR_DIM = 2;
|
|
const COLOR_RESET = '\x1b[0m';
|
|
const MAX_LOG_ENTRIES = 1000;
|
|
const logEntries: LogEntry[] = [];
|
|
const logSubscribers = new Set<(entry: LogEntry) => void>();
|
|
let nextLogId = 1;
|
|
|
|
function resolveMinLevel(): LogLevel {
|
|
const raw = (process.env.SNOWLUMA_LOG_LEVEL ?? 'info').toLowerCase();
|
|
if (raw === 'debug' || raw === 'info' || raw === 'success' || raw === 'warn' || raw === 'error') {
|
|
return raw;
|
|
}
|
|
return 'info';
|
|
}
|
|
|
|
const MIN_LEVEL = resolveMinLevel();
|
|
|
|
function shouldLog(level: LogLevel): boolean {
|
|
return LEVEL_WEIGHT[level] >= LEVEL_WEIGHT[MIN_LEVEL];
|
|
}
|
|
|
|
function useColor(): boolean {
|
|
if (process.env.NO_COLOR === '1') return false;
|
|
return Boolean(process.stdout.isTTY);
|
|
}
|
|
|
|
function ansi(code: number, text: string): string {
|
|
return `\x1b[${code}m${text}${COLOR_RESET}`;
|
|
}
|
|
|
|
function currentTime(): string {
|
|
const d = new Date();
|
|
const hh = String(d.getHours()).padStart(2, '0');
|
|
const mm = String(d.getMinutes()).padStart(2, '0');
|
|
const ss = String(d.getSeconds()).padStart(2, '0');
|
|
return `${hh}:${mm}:${ss}`;
|
|
}
|
|
|
|
function render(level: LogLevel, scope: string, args: unknown[]): string {
|
|
const message = format(...args);
|
|
const ts = currentTime();
|
|
const label = LEVEL_LABEL[level].padEnd(5, ' ');
|
|
|
|
if (!useColor()) {
|
|
return `${ts} ${label} [${scope}] ${message}`;
|
|
}
|
|
|
|
const cTs = ansi(COLOR_DIM, ts);
|
|
const cLabel = ansi(COLOR_CODE[level], label);
|
|
const cScope = ansi(COLOR_SCOPE, `[${scope}]`);
|
|
return `${cTs} ${cLabel} ${cScope} ${message}`;
|
|
}
|
|
|
|
function emit(level: LogLevel, options: LogOptions, args: unknown[]): void {
|
|
if (!shouldLog(level)) return;
|
|
const message = format(...args);
|
|
const line = render(level, options.scope, args);
|
|
const entry: LogEntry = {
|
|
id: nextLogId++,
|
|
time: new Date().toISOString(),
|
|
level,
|
|
scope: options.scope,
|
|
message,
|
|
line,
|
|
};
|
|
logEntries.push(entry);
|
|
if (logEntries.length > MAX_LOG_ENTRIES) logEntries.shift();
|
|
for (const subscriber of logSubscribers) subscriber(entry);
|
|
const stream = level === 'warn' || level === 'error' ? process.stderr : process.stdout;
|
|
stream.write(line + '\n');
|
|
}
|
|
|
|
export function getRecentLogs(limit = 300): LogEntry[] {
|
|
const n = Math.max(1, Math.min(Math.trunc(limit), MAX_LOG_ENTRIES));
|
|
return logEntries.slice(-n);
|
|
}
|
|
|
|
export function subscribeLogs(callback: (entry: LogEntry) => void): () => void {
|
|
logSubscribers.add(callback);
|
|
return () => {
|
|
logSubscribers.delete(callback);
|
|
};
|
|
}
|
|
|
|
export interface Logger {
|
|
debug: (...args: unknown[]) => void;
|
|
info: (...args: unknown[]) => void;
|
|
success: (...args: unknown[]) => void;
|
|
warn: (...args: unknown[]) => void;
|
|
error: (...args: unknown[]) => void;
|
|
}
|
|
|
|
export function createLogger(scope: string): Logger {
|
|
const options: LogOptions = { scope };
|
|
return {
|
|
debug: (...args: unknown[]) => emit('debug', options, args),
|
|
info: (...args: unknown[]) => emit('info', options, args),
|
|
success: (...args: unknown[]) => emit('success', options, args),
|
|
warn: (...args: unknown[]) => emit('warn', options, args),
|
|
error: (...args: unknown[]) => emit('error', options, args),
|
|
};
|
|
}
|