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

+4 -7
View File
@@ -21,7 +21,6 @@ import crypto from "crypto";
import yargs from "yargs";
import util from "util";
import winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
// homebrew modules
@@ -355,19 +354,17 @@ async function initialize(): Promise<InitializeParameters> {
level: "verbose",
levels,
});
clusterLogger.add(new DailyRotateFile({
clusterLogger.add(new lib.DailyFileTransport({
format: winston.format.json(),
filename: "cluster-%DATE%.log",
utc: true,
dirname: path.join(args.logDirectory, "cluster"),
prefix: "cluster",
}));
// Log stream for the controller.
logger.add(new DailyRotateFile({
logger.add(new lib.DailyFileTransport({
format: winston.format.json(),
filename: "controller-%DATE%.log",
utc: true,
dirname: path.join(args.logDirectory, "controller"),
prefix: "controller",
}));
logger.add(new winston.transports.Stream({
stream: clusterLogger,
-1
View File
@@ -45,7 +45,6 @@
"jsonwebtoken": "catalog:",
"semver": "catalog:",
"winston": "catalog:",
"winston-daily-rotate-file": "catalog:",
"ws": "catalog:",
"yargs": "catalog:"
},
+2 -4
View File
@@ -17,7 +17,6 @@ import path from "path";
import yargs from "yargs";
import packageConfig from "@clusterio/host/package.json" with { type: "json" };
import winston from "winston";
import "winston-daily-rotate-file";
// internal libraries
// Reduce startup time by lazy compiling schemas.
@@ -147,11 +146,10 @@ async function startHost() {
.parseSync()
;
logger.add(new winston.transports.DailyRotateFile({
logger.add(new lib.DailyFileTransport({
format: winston.format.json(),
filename: "host-%DATE%.log",
utc: true,
dirname: path.join(args.logDirectory, "host"),
prefix: "host",
}));
if (args.logLevel !== "none") {
logger.add(new ConsoleTransport({
-1
View File
@@ -49,7 +49,6 @@
"semver": "catalog:",
"set-blocking": "catalog:",
"winston": "catalog:",
"winston-daily-rotate-file": "catalog:",
"yargs": "catalog:",
"yazl": "catalog:"
},
+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;
-1
View File
@@ -33,7 +33,6 @@ catalog:
typescript: ^7.0.2
vite: ^8.3.4
winston: ^3.19.0
winston-daily-rotate-file: ^5.0.0
winston-transport: ^4.9.0
ws: ^8.22.0
yargs: ^18.2.0
+72
View File
@@ -1,5 +1,6 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { testLines } from "./factorio/lines.js";
@@ -66,6 +67,77 @@ describe("lib/logging_utils.js", function() {
});
});
describe("class DailyFileTransport", function() {
let tempDir = path.join("temp", "test", "daily_file");
beforeEach(async function() {
await fs.rm(tempDir, { recursive: true, force: true });
});
function create(now, dirname = tempDir) {
return new lib.DailyFileTransport({ dirname, prefix: "test", now });
}
function write(transport, message) {
transport.log({ [Symbol.for("message")]: JSON.stringify({ message }) }, () => {});
}
async function close(transport) {
await new Promise(resolve => transport.close(resolve));
}
async function read(file) {
let content = await fs.readFile(path.join(tempDir, file), "utf8");
assert(content.endsWith(os.EOL));
return content.split(os.EOL).slice(0, -1).map(line => JSON.parse(line).message);
}
it("should create the directory and write lines to the file for the UTC date", async function() {
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"), path.join(tempDir, "nested"));
write(transport, "first");
write(transport, "second");
await close(transport);
assert.deepEqual(await read(path.join("nested", "test-2026-11-19.log")), ["first", "second"]);
});
it("should append to an existing file", async function() {
await fs.mkdir(tempDir, { recursive: true });
await fs.writeFile(path.join(tempDir, "test-2026-11-19.log"), `{"message":"old"}${os.EOL}`);
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"));
write(transport, "new");
await close(transport);
assert.deepEqual(await read("test-2026-11-19.log"), ["old", "new"]);
});
it("should switch files when the UTC date changes", async function() {
let time = Date.parse("2026-11-19T23:59:59.999Z");
let transport = create(() => time);
write(transport, "before");
time += 1;
write(transport, "after");
await close(transport);
assert.deepEqual((await fs.readdir(tempDir)).sort(), ["test-2026-11-19.log", "test-2026-11-20.log"]);
assert.deepEqual(await read("test-2026-11-19.log"), ["before"]);
assert.deepEqual(await read("test-2026-11-20.log"), ["after"]);
});
it("should use the UTC date rather than the local one", async function() {
let transport = create(() => Date.parse("2026-11-20T00:30:00+02:00"));
write(transport, "message");
await close(transport);
assert.deepEqual(await fs.readdir(tempDir), ["test-2026-11-19.log"]);
});
it("should emit write errors", async function() {
await fs.mkdir(path.join(tempDir, "test-2026-11-19.log"), { recursive: true });
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"));
let error = new Promise(resolve => transport.once("error", resolve));
write(transport, "message");
assert.equal((await error).code, "EISDIR");
});
it("should close when nothing was written", async function() {
await close(create(Date.now));
assert.deepEqual(await fs.readdir(tempDir), []);
});
});
describe("deleteOldLogs", function() {
let tempDir = path.join("temp", "test", "log_retention");
let files = [