feat(logger): runtime log level toggle via WebUI

The console / subscriber level was captured at module load
(`const MIN_LEVEL = resolveMinLevel()`), so any change to
SNOWLUMA_LOG_LEVEL after Node started had no effect — restart was
the only way to flip levels, which makes "go to debug for 30s to
see what's happening" pretty painful.

logger.ts now keeps `currentLevel` mutable and exports
setLogLevel() / getLogLevel(). The env var still seeds it on
startup; nothing about the boot-time behavior changes. File
transport remains unaffected — it always sees debug-and-up.

WebUI gets two new endpoints, both behind the existing auth
middleware:
  GET  /api/logs/level       → { level, levels }
  POST /api/logs/level body  → { level: 'debug' } updates it

Logs page picks up a "服务端级别" row of pills next to the
existing client-side filter pills, so an operator can flip the
console firehose on/off in one click without touching env vars
or restarting the bot. The label clarifies that file logging
stays at debug regardless.
This commit is contained in:
DrSmoothl committed 2026-05-18 13:15:05 +08:00
1 parent 4a49c55775
commit 4c513c015d
7 files changed
+154 -12

No files matched your search

+4 -1
View File
@@ -1,4 +1,4 @@
import type { HookProcessInfo, LogEntry, QQInfo, SystemInfo } from '@/types';
import type { HookProcessInfo, LogEntry, LogLevel, QQInfo, SystemInfo } from '@/types';
import type { PasswordRule } from '@/components/pages/change-password-page';
import { normalizeOneBotConfig } from '@/lib/onebot-config';
import {
@@ -84,6 +84,9 @@ class HttpApiClient implements ApiClient {
return data.list ?? [];
},
stream: (options) => this.openLogStream(options),
getLevel: () => this.getJson<{ level: LogLevel; levels: LogLevel[] }>(`/api/logs/level`),
setLevel: (level) =>
this.postJson<{ level: LogLevel; levels: LogLevel[] }>(`/api/logs/level`, { level }),
};
}
+5
View File
@@ -1,6 +1,7 @@
import type {
HookProcessInfo,
LogEntry,
LogLevel,
OneBotConfig,
QQInfo,
SystemInfo,
@@ -69,6 +70,10 @@ export interface ApiClient {
list(limit?: number): Promise<LogEntry[]>;
/** Subscribe to the SSE log stream. Returns a disposer. */
stream(options: LogsStreamOptions): () => void;
/** Current console / subscriber level. File output is always debug. */
getLevel(): Promise<{ level: LogLevel; levels: LogLevel[] }>;
/** Change the console / subscriber level at runtime. No restart needed. */
setLevel(level: LogLevel): Promise<{ level: LogLevel; levels: LogLevel[] }>;
};
/** Escape hatch for endpoints not yet wrapped above. */