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

+48 -1
View File
@@ -11,7 +11,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createLogger, subscribeLogs, type LogEntry } from '../src/utils/logger';
import { createLogger, getLogLevel, setLogLevel, subscribeLogs, type LogEntry } from '../src/utils/logger';
import { _resetFileTransportForTesting } from '../src/utils/log-file-transport';
let tmpDir: string;
@@ -178,3 +178,50 @@ describe('createLogger', () => {
expect(entries).toHaveLength(0);
});
});
describe('setLogLevel / getLogLevel — runtime level toggle', () => {
// Each test restores whatever level was active going in so the suite
// ordering can't bleed state between specs.
let saved: ReturnType<typeof getLogLevel>;
beforeEach(() => { saved = getLogLevel(); });
afterEach(() => { setLogLevel(saved); });
it('setLogLevel updates getLogLevel for valid input', () => {
expect(setLogLevel('warn')).toBe(true);
expect(getLogLevel()).toBe('warn');
expect(setLogLevel('debug')).toBe(true);
expect(getLogLevel()).toBe('debug');
});
it('setLogLevel rejects unknown values without mutating state', () => {
setLogLevel('info');
expect(setLogLevel('nonsense')).toBe(false);
expect(getLogLevel()).toBe('info');
expect(setLogLevel('')).toBe(false);
expect(setLogLevel('Verbose')).toBe(false);
});
it('setLogLevel is case-insensitive', () => {
expect(setLogLevel('WARN')).toBe(true);
expect(getLogLevel()).toBe('warn');
});
it('changes which entries reach the ring buffer / subscribers', () => {
const captured: LogEntry[] = [];
const unsub = subscribeLogs((e) => captured.push(e));
const log = createLogger('LevelTest');
setLogLevel('warn');
log.debug('hidden-debug');
log.info('hidden-info');
log.warn('visible-warn');
log.error('visible-error');
unsub();
const messages = captured
.filter((e) => e.scope === 'LevelTest')
.map((e) => e.message);
expect(messages).toEqual(['visible-warn', 'visible-error']);
});
});