Merge pull request #1234 from bbassie/chore/inline-daily-rotate

Replace winston-daily-rotate-file with an in-repo transport
This commit is contained in:
Bastiaan authored and GitHub committed 2026-11-20 20:54:42 +00:00
1 parent 81c7d24867
commit 1c18dc3661
7 files changed
+129 -15

No files matched your search

+51 -1
View File
@@ -5,7 +5,8 @@
* @module lib/logging_utils
*/
import fs from "node:fs/promises";
import type { ReadStream } from "node:fs";
import { type ReadStream, type WriteStream, createWriteStream, mkdirSync } from "node:fs";
import os from "node:os";
import path from "path";
import stream from "stream";
import util, { styleText } from "util";
@@ -225,6 +226,55 @@ export class LinkTransport extends Transport {
}
}
/**
* Writes log entries as lines to dirname/prefix-YYYY-MM-DD.log, opening
* a new file when the UTC date changes.
*/
export class DailyFileTransport extends Transport {
dirname: string;
prefix: string;
now: () => number;
date?: string;
stream?: WriteStream;
constructor(
options: Transport.TransportStreamOptions & {
dirname: string,
prefix: string,
now?: () => number,
}
) {
super(options);
this.dirname = options.dirname;
this.prefix = options.prefix;
this.now = options.now ?? Date.now;
// eslint-disable-next-line node/no-sync
mkdirSync(this.dirname, { recursive: true });
}
log(info: any, callback: () => void) {
const date = new Date(this.now()).toISOString().slice(0, 10);
if (!this.stream || date !== this.date) {
this.stream?.end();
this.date = date;
this.stream = createWriteStream(path.join(this.dirname, `${this.prefix}-${date}.log`), { flags: "a" });
this.stream.on("error", err => this.emit("error", err));
}
this.stream.write(info[MESSAGE] + os.EOL);
return callback();
}
close(callback?: () => void) {
const fileStream = this.stream;
this.stream = undefined;
if (fileStream) {
fileStream.end(callback);
} else {
callback?.();
}
}
}
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
const logIndexVersion = 2;