feat(logger): add file transport with daily rotation and size cap

Adds a hand-rolled FileTransport that fans out every log line (debug
and up, regardless of console filter) to
./logs/snowluma-YYYY-MM-DD.log. When the current file exceeds the
configurable size cap it rolls into snowluma-YYYY-MM-DD.1.log, and
files older than the retention window are unlinked at startup and on
each day rollover.

Config (all env, no settings.json):
- SNOWLUMA_LOG_FILE      0 to disable file output (default: on)
- SNOWLUMA_LOG_DIR       output directory       (default: ./logs)
- SNOWLUMA_LOG_MAX_MB    per-file cap, MB       (default: 50)
- SNOWLUMA_LOG_RETAIN_DAYS retention window     (default: 7)

closeLogger() awaits the WriteStream's finish event; SIGINT and
SIGTERM handlers now await it before process.exit so the buffer
makes it to disk.

vitest config sets SNOWLUMA_LOG_FILE=0 to keep the suite from
polluting cwd; the new tests under tests/log-file-transport.test.ts
flip it back on per-test against a tmpdir.
This commit is contained in:
DrSmoothl committed 2026-05-18 00:44:30 +08:00
1 parent c6f4d5c5a2
commit 51f46e15fd
5 files changed
+445 -18

No files matched your search

@@ -0,0 +1,236 @@
// File transport for the SnowLuma logger.
//
// Writes plain text (ANSI stripped) to:
// <SNOWLUMA_LOG_DIR>/snowluma-YYYY-MM-DD.log
// with daily rollover and a per-file size cap. When the cap is hit the
// in-flight file is closed and writes continue in
// snowluma-YYYY-MM-DD.1.log, .2.log, ...
// at the same date. Files older than the retain window are unlinked on
// startup and again on each day rollover.
//
// Disabled via SNOWLUMA_LOG_FILE=0. Any I/O failure during init or write
// degrades silently to console-only; we deliberately do NOT use the
// logger from inside this module to avoid recursion.
import fs from 'node:fs';
import path from 'node:path';
const DEFAULT_DIR = 'logs';
const DEFAULT_MAX_MB = 50;
const DEFAULT_RETAIN_DAYS = 7;
const FILE_PREFIX = 'snowluma-';
const FILE_SUFFIX = '.log';
const FILE_RE = /^snowluma-(\d{4}-\d{2}-\d{2})(?:\.(\d+))?\.log$/;
// eslint-disable-next-line no-control-regex
const ANSI_RE = /\x1B\[[0-?]*[ -/]*[@-~]/g;
// eslint-disable-next-line no-control-regex
const CTRL_RE = /[\x00-\x1A\x1C-\x1F\x7F]/g;
function parsePositiveInt(v: string | undefined, dflt: number): number {
if (!v) return dflt;
const n = Number.parseInt(v, 10);
return Number.isFinite(n) && n > 0 ? n : dflt;
}
function todayString(d: Date = new Date()): string {
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${y}-${m}-${day}`;
}
function dateOf(s: string): Date {
const [y, m, d] = s.split('-').map((v) => Number.parseInt(v, 10));
return new Date(y!, m! - 1, d!);
}
function stripAnsi(line: string): string {
return line.replace(ANSI_RE, '').replace(CTRL_RE, '');
}
interface OpenFile {
stream: fs.WriteStream;
bytes: number;
date: string;
splitIndex: number;
path: string;
}
export class FileTransport {
private disabled = false;
private readonly dir: string;
private readonly maxBytes: number;
private readonly retainDays: number;
private shared: OpenFile | null = null;
constructor() {
this.dir = process.env.SNOWLUMA_LOG_DIR || DEFAULT_DIR;
this.maxBytes =
parsePositiveInt(process.env.SNOWLUMA_LOG_MAX_MB, DEFAULT_MAX_MB) * 1024 * 1024;
this.retainDays = parsePositiveInt(
process.env.SNOWLUMA_LOG_RETAIN_DAYS,
DEFAULT_RETAIN_DAYS,
);
if (process.env.SNOWLUMA_LOG_FILE === '0') {
this.disabled = true;
return;
}
try {
fs.mkdirSync(this.dir, { recursive: true });
} catch (err) {
this.disabled = true;
// Direct stderr — logger would recurse.
// eslint-disable-next-line no-console
console.error(
`[logger] failed to create log dir ${this.dir}: ${err instanceof Error ? err.message : String(err)}`,
);
return;
}
this.cleanup();
}
/** True when file output is suppressed (env disable or init failure). */
get isDisabled(): boolean {
return this.disabled;
}
/** Current target file path (or null if disabled / not yet opened). */
get currentPath(): string | null {
return this.shared?.path ?? null;
}
write(line: string): void {
if (this.disabled) return;
const today = todayString();
const data = stripAnsi(line) + '\n';
const bytes = Buffer.byteLength(data, 'utf8');
this.ensureForToday(today);
if (!this.shared) return;
if (this.shared.bytes + bytes > this.maxBytes && this.shared.bytes > 0) {
this.rotateBySize();
if (!this.shared) return;
}
this.shared.stream.write(data);
this.shared.bytes += bytes;
}
close(): Promise<void> {
const f = this.shared;
this.shared = null;
if (!f) return Promise.resolve();
return new Promise((resolve) => {
try {
f.stream.end(() => resolve());
} catch {
resolve();
}
});
}
private ensureForToday(today: string): void {
if (this.shared && this.shared.date === today) return;
if (this.shared) {
try {
this.shared.stream.end();
} catch {
/* ignore */
}
this.shared = null;
// Day rolled over — also a good moment to evict stale files.
this.cleanup();
}
// Find the highest existing split index for today and continue appending
// to it (so multiple process starts on the same day share files).
let idx = 0;
while (fs.existsSync(this.pathFor(today, idx + 1))) idx++;
this.shared = this.openFile(today, idx);
}
private rotateBySize(): void {
if (!this.shared) return;
const date = this.shared.date;
try {
this.shared.stream.end();
} catch {
/* ignore */
}
let next = this.shared.splitIndex + 1;
while (fs.existsSync(this.pathFor(date, next))) next++;
this.shared = this.openFile(date, next);
}
private openFile(date: string, splitIndex: number): OpenFile | null {
const p = this.pathFor(date, splitIndex);
try {
let bytes = 0;
try {
bytes = fs.statSync(p).size;
} catch {
/* file doesn't exist yet */
}
const stream = fs.createWriteStream(p, { flags: 'a' });
stream.on('error', (err) => {
// eslint-disable-next-line no-console
console.error(`[logger] file write error on ${p}: ${err.message}`);
});
return { stream, bytes, date, splitIndex, path: p };
} catch (err) {
// eslint-disable-next-line no-console
console.error(
`[logger] failed to open log file ${p}: ${err instanceof Error ? err.message : String(err)}`,
);
return null;
}
}
private pathFor(date: string, splitIndex: number): string {
const tail = splitIndex > 0 ? `.${splitIndex}` : '';
return path.join(this.dir, `${FILE_PREFIX}${date}${tail}${FILE_SUFFIX}`);
}
private cleanup(): void {
let entries: string[];
try {
entries = fs.readdirSync(this.dir);
} catch {
return;
}
const retainMs = this.retainDays * 24 * 60 * 60 * 1000;
const cutoff = Date.now() - retainMs;
for (const name of entries) {
const m = FILE_RE.exec(name);
if (!m) continue;
const dateStr = m[1]!;
const fileDate = dateOf(dateStr);
if (fileDate.getTime() < cutoff) {
try {
fs.unlinkSync(path.join(this.dir, name));
} catch {
/* ignore */
}
}
}
}
}
let singleton: FileTransport | null = null;
export function getFileTransport(): FileTransport {
if (!singleton) singleton = new FileTransport();
return singleton;
}
/** Reset state between tests. Closes the active file handle. */
export async function _resetFileTransportForTesting(): Promise<void> {
if (singleton) await singleton.close();
singleton = null;
}
+31 -11
View File
@@ -1,4 +1,5 @@
import { format } from 'util';
import { getFileTransport } from './log-file-transport';
type LogLevel = 'debug' | 'info' | 'success' | 'warn' | 'error';
@@ -94,7 +95,9 @@ function render(level: LogLevel, scope: string, args: unknown[]): string {
}
function emit(level: LogLevel, options: LogOptions, args: unknown[]): void {
if (!shouldLog(level)) return;
// Console / subscriber level filter. File output is debug-and-up always;
// see log-file-transport.ts.
const passesConsole = shouldLog(level);
const message = format(...args);
const line = render(level, options.scope, args);
const entry: LogEntry = {
@@ -105,16 +108,33 @@ function emit(level: LogLevel, options: LogOptions, args: unknown[]): void {
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;
// Strip ASCII control characters before writing to terminal to prevent
// BEL (0x07) in user-provided strings (e.g. group member names) from
// triggering Windows system beep sounds. ESC (0x1B) is preserved so the
// ANSI color sequences emitted by render() actually reach the terminal.
// eslint-disable-next-line no-control-regex
stream.write(line.replace(/[\x00-\x1A\x1C-\x1F\x7F]/g, '') + '\n');
if (passesConsole) {
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;
// Strip ASCII control characters before writing to terminal to prevent
// BEL (0x07) in user-provided strings (e.g. group member names) from
// triggering Windows system beep sounds. ESC (0x1B) is preserved so the
// ANSI color sequences emitted by render() actually reach the terminal.
// eslint-disable-next-line no-control-regex
stream.write(line.replace(/[\x00-\x1A\x1C-\x1F\x7F]/g, '') + '\n');
}
// File transport always sees every level (debug-and-up) for post-mortem
// value; ANSI stripping happens inside the transport.
getFileTransport().write(line);
}
/**
* Flush and close the underlying log file. Call from shutdown hooks
* (SIGINT / SIGTERM / uncaughtException) so the WriteStream's internal
* buffer makes it to disk. Returns a promise that resolves once the OS
* has finalized the write.
*/
export function closeLogger(): Promise<void> {
return getFileTransport().close();
}
export function getRecentLogs(limit = 300): LogEntry[] {