Skip to content

Commit 7c511b3

Browse files
fix(shared): preserve files outside log rotation naming (#27503)
Co-authored-by: maria-rcks <[email protected]>
1 parent 5e2a331 commit 7c511b3

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎packages/shared/src/logging.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,36 @@ afterEach(() => {
3333
}
3434
});
3535

36-
effectIt.layer(NodeServices.layer)("asynchronous rotating file sink", (it) => {
36+
effectIt.layer(NodeServices.layer)("rotating file sinks", (it) => {
37+
it.effect.each(["synchronous", "asynchronous"] as const)(
38+
"%s startup prunes only canonical rotation suffixes",
39+
(mode) =>
40+
Effect.gen(function* () {
41+
const directory = makeTempDirectory();
42+
const filePath = NodePath.join(directory, "debug.log");
43+
const retained = ["1", "3", "04", "4.0", "4e0", "0x4", "+4", "notes"];
44+
for (const suffix of [...retained, "4", "50"]) {
45+
NodeFS.writeFileSync(`${filePath}.${suffix}`, suffix);
46+
}
47+
const options = { filePath, maxBytes: 100, maxFiles: 3 };
48+
49+
const sink =
50+
mode === "synchronous"
51+
? new RotatingFileSink(options)
52+
: yield* makeAsyncRotatingFileSink(options);
53+
if (sink instanceof RotatingFileSink) sink.write("current");
54+
else yield* sink.write("current");
55+
56+
expect(NodeFS.readdirSync(directory).sort()).toEqual(
57+
["debug.log", ...retained.map((suffix) => `debug.log.${suffix}`)].sort(),
58+
);
59+
expect(NodeFS.readFileSync(filePath, "utf8")).toBe("current");
60+
for (const suffix of retained) {
61+
expect(NodeFS.readFileSync(`${filePath}.${suffix}`, "utf8")).toBe(suffix);
62+
}
63+
}),
64+
);
65+
3766
it.effect("serializes concurrent appends across rotation and resumes an existing file", () =>
3867
Effect.gen(function* () {
3968
const directory = makeTempDirectory();

‎packages/shared/src/logging.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ export class RotatingFileSink {
150150
const baseName = NodePath.basename(this.filePath);
151151
for (const entry of NodeFS.readdirSync(dir)) {
152152
if (!entry.startsWith(`${baseName}.`)) continue;
153-
const suffix = Number(entry.slice(baseName.length + 1));
154-
if (!Number.isInteger(suffix) || suffix <= this.maxFiles) continue;
153+
const suffixText = entry.slice(baseName.length + 1);
154+
const suffix = Number(suffixText);
155+
if (!Number.isInteger(suffix) || String(suffix) !== suffixText || suffix <= this.maxFiles)
156+
continue;
155157
NodeFS.rmSync(NodePath.join(dir, entry), { force: true });
156158
}
157159
} catch (cause) {
@@ -212,8 +214,10 @@ export const makeAsyncRotatingFileSink = Effect.fnUntraced(function* (
212214
yield* Effect.gen(function* () {
213215
for (const entry of yield* fs.readDirectory(directory)) {
214216
if (!entry.startsWith(`${basename}.`)) continue;
215-
const suffix = Number(entry.slice(basename.length + 1));
216-
if (!Number.isInteger(suffix) || suffix <= options.maxFiles) continue;
217+
const suffixText = entry.slice(basename.length + 1);
218+
const suffix = Number(suffixText);
219+
if (!Number.isInteger(suffix) || String(suffix) !== suffixText || suffix <= options.maxFiles)
220+
continue;
217221
yield* fs.remove(path.join(directory, entry), { force: true });
218222
}
219223
}).pipe(Effect.mapError(failure("prune")));

0 commit comments

Comments
 (0)