Skip to content

Commit

Permalink
fix: Fix log directories not being deleted (#25282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk authored Dec 21, 2024
1 parent 758ab34 commit 7170215
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Logger {

this.fileTransport = new winston.transports.File(transportFileOptions);
this.logger.add(this.fileTransport);
this.cleanup();
}

/* istanbul ignore next */
Expand Down Expand Up @@ -219,7 +220,7 @@ class Logger {
}

// Cleanup any old log directory.
public cleanup(): void {
private cleanup(): void {
if (settings.get().advanced.log_directory.includes('%TIMESTAMP%')) {
const rootDirectory = path.join(this.directory, '..');

Expand Down
16 changes: 10 additions & 6 deletions test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,27 @@ describe('Logger', () => {
rimrafSync(path.join(dir.name, d));
}

for (let i = 0; i < 21; i++) {
for (let i = 0; i < 20; i++) {
fs.mkdirSync(path.join(dir.name, `log_${i}`));
}

expect(fs.readdirSync(dir.name).length).toBe(21);
logger.cleanup();
expect(fs.readdirSync(dir.name).length).toBe(20);
logger.init();
expect(fs.readdirSync(dir.name).length).toBe(10);
});

it('Should not cleanup when there is no timestamp set', () => {
for (let i = 30; i < 40; i++) {
for (const d of fs.readdirSync(dir.name)) {
rimrafSync(path.join(dir.name, d));
}

for (let i = 30; i < 50; i++) {
fs.mkdirSync(path.join(dir.name, `log_${i}`));
}

settings.set(['advanced', 'log_directory'], dir.name + '/bla');
expect(fs.readdirSync(dir.name).length).toBe(21);
logger.cleanup();
expect(fs.readdirSync(dir.name).length).toBe(20);
logger.init();
expect(fs.readdirSync(dir.name).length).toBe(21);
});

Expand Down

0 comments on commit 7170215

Please sign in to comment.