fix(logging): harden file level configuration

Make persisted log filtering predictable across supported startup settings while rejecting configurations that could silently retain more data than intended.

Keep the first WebUI login credential available without weakening normal log thresholds, clarify the operator-facing behavior, and strengthen regression coverage across isolated environments and explicit file-output disablement.
This commit is contained in:
DrSmoothl committed 2026-07-30 10:27:43 +08:00
1 parent 227a62f2ad
commit 655226d9d7
6 files changed
+212 -181

No files matched your search

+39 -2
View File
@@ -7,11 +7,30 @@
// We exercise the file transport via its real fs paths against tmpdirs
// so the test catches integration regressions, not just unit shape.
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createLogger, getLogLevel, setLogLevel, subscribeLogs, type LogEntry } from '@snowluma/common/logger';
const { externalFileLevel, externalLogLevel } = vi.hoisted(() => {
const savedFileLevel = process.env.SNOWLUMA_LOG_FILE_LEVEL;
const savedLogLevel = process.env.SNOWLUMA_LOG_LEVEL;
delete process.env.SNOWLUMA_LOG_FILE_LEVEL;
delete process.env.SNOWLUMA_LOG_LEVEL;
return {
externalFileLevel: savedFileLevel,
externalLogLevel: savedLogLevel,
};
});
import {
createLogger,
getLogLevel,
logInitialWebuiCredentials,
setLogLevel,
subscribeLogs,
type LogEntry,
} from '@snowluma/common/logger';
import {
_resetFileTransportForTesting,
configureFileTransport,
@@ -26,6 +45,7 @@ const ENV_KEYS = [
'SNOWLUMA_LOG_MAX_TOTAL_MB',
'SNOWLUMA_LOG_RETAIN_DAYS',
'SNOWLUMA_LOG_LEVEL',
'SNOWLUMA_LOG_FILE_LEVEL',
'NO_COLOR',
] as const;
const savedEnv: Record<string, string | undefined> = {};
@@ -61,6 +81,13 @@ afterEach(async () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
afterAll(() => {
if (externalFileLevel === undefined) delete process.env.SNOWLUMA_LOG_FILE_LEVEL;
else process.env.SNOWLUMA_LOG_FILE_LEVEL = externalFileLevel;
if (externalLogLevel === undefined) delete process.env.SNOWLUMA_LOG_LEVEL;
else process.env.SNOWLUMA_LOG_LEVEL = externalLogLevel;
});
describe('createLogger', () => {
it('renders [Scope] with the empty UIN slot when no child binding', async () => {
const log = createLogger('Bridge');
@@ -183,6 +210,16 @@ describe('createLogger', () => {
expect(entries).toHaveLength(0);
});
it('SNOWLUMA_LOG_FILE=0 suppresses bootstrap notices too', async () => {
setEnv({ SNOWLUMA_LOG_FILE: '0' });
await _resetFileTransportForTesting();
logInitialWebuiCredentials('secret');
await _resetFileTransportForTesting();
expect(fs.readdirSync(tmpDir)).toHaveLength(0);
});
it('applies the runtime storage policy through the singleton before writing', async () => {
const status = await configureFileTransport({
maxTotalMb: 3,