Skip to content

Commit 522d1f4

Browse files
kriszypclaude
andcommitted
fix(logging): scope logRotator state per instance
`lastRotationTime` and `setIntervalId` were module-level. After #1111 fixed the broken require so harper_logger.getFileLogger actually creates rotators, multiple rotators can exist concurrently (e.g. for http logger, external logger, hdb logger). Every new rotator call overwrites the shared `setIntervalId`, so `rotator.end()` on any instance clears the most-recently-created interval — not its own. This surfaced as a unit-test regression: the harper_logger HTTP and global-logger tests trigger lingering setTimeouts (from the `path =` setter calling getFileLogger) that fire ~100ms later. When the logRotator test then created its rotator, the lingering setTimeouts called `logger.rotator?.end()` -> `clearInterval(module-level setIntervalId)`, which clobbered the test's interval. The rotation never fired and `getLastRotatedLogPath()` returned undefined. Move both variables into the function closure so each rotator owns its own state and `end()` only clears its own interval. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent bce7e4d commit 522d1f4

1 file changed

Lines changed: 2 additions & 5 deletions

File tree

utility/logging/logRotator.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ const LOG_AUDIT_INTERVAL = 60000;
1818
const INT_SIZE_UNDEFINED_MSG =
1919
"'interval' and 'maxSize' are both undefined, to enable logging rotation at least one of these values must be defined in harperdb-config.yaml";
2020

21-
let lastRotationTime;
22-
let setIntervalId;
23-
2421
export { logRotator };
2522

2623
/**
@@ -67,9 +64,9 @@ function logRotator({ logger, maxSize, interval, retention, enabled, path: rotat
6764

6865
let lastRotatedLogPath;
6966
// convert date.now to minutes
70-
lastRotationTime = Date.now();
67+
let lastRotationTime = Date.now();
7168
hdbLogger.trace('Log rotate enabled, maxSize:', maxSize, 'interval:', interval);
72-
setIntervalId = setInterval(async () => {
69+
const setIntervalId = setInterval(async () => {
7370
if (maxBytes) {
7471
let fileStats;
7572
try {

0 commit comments

Comments
 (0)