Skip to content

Commit 02eb784

Browse files
committed
Fix microsoft#79882 Recurring web logs
1 parent ef7a57a commit 02eb784

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/vs/platform/log/common/fileLogService.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import { URI } from 'vs/base/common/uri';
88
import { IFileService } from 'vs/platform/files/common/files';
99
import { Queue } from 'vs/base/common/async';
1010
import { VSBuffer } from 'vs/base/common/buffer';
11+
import { dirname, joinPath, basename } from 'vs/base/common/resources';
12+
13+
const MAX_FILE_SIZE = 1024 * 1024 * 5;
1114

1215
export class FileLogService extends AbstractLogService implements ILogService {
1316

1417
_serviceBrand: undefined;
1518

1619
private readonly queue: Queue<void>;
20+
private backupIndex: number = 1;
1721

1822
constructor(
1923
private readonly name: string,
@@ -81,6 +85,10 @@ export class FileLogService extends AbstractLogService implements ILogService {
8185
private _log(level: LogLevel, message: string): void {
8286
this.queue.queue(async () => {
8387
let content = await this.loadContent();
88+
if (content.length > MAX_FILE_SIZE) {
89+
await this.fileService.writeFile(this.getBackupResource(), VSBuffer.fromString(content));
90+
content = '';
91+
}
8492
content += `[${this.getCurrentTimestamp()}] [${this.name}] [${this.stringifyLogLevel(level)}] ${message}\n`;
8593
await this.fileService.writeFile(this.resource, VSBuffer.fromString(content));
8694
});
@@ -93,6 +101,11 @@ export class FileLogService extends AbstractLogService implements ILogService {
93101
return `${currentTime.getFullYear()}-${toTwoDigits(currentTime.getMonth() + 1)}-${toTwoDigits(currentTime.getDate())} ${toTwoDigits(currentTime.getHours())}:${toTwoDigits(currentTime.getMinutes())}:${toTwoDigits(currentTime.getSeconds())}.${toThreeDigits(currentTime.getMilliseconds())}`;
94102
}
95103

104+
private getBackupResource(): URI {
105+
this.backupIndex = this.backupIndex > 5 ? 1 : this.backupIndex;
106+
return joinPath(dirname(this.resource), `${basename(this.resource)}_${this.backupIndex++}`);
107+
}
108+
96109
private async loadContent(): Promise<string> {
97110
try {
98111
const content = await this.fileService.readFile(this.resource);

0 commit comments

Comments
 (0)