文件
mynode/packages/lib/RotatingLogs.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

48 行
1.5 KiB
TypeScript

import shim from './shim';
import { Stat } from './fs-driver-base';
export default class RotatingLogs {
private logFilesDir: string;
private maxFileSize: number = 1024 * 1024 * 100;
private inactiveMaxAge: number = 90 * 24 * 60 * 60 * 1000;
public constructor(logFilesDir: string, maxFileSize: number = null, inactiveMaxAge: number = null) {
this.logFilesDir = logFilesDir;
if (maxFileSize) this.maxFileSize = maxFileSize;
if (inactiveMaxAge) this.inactiveMaxAge = inactiveMaxAge;
}
public async cleanActiveLogFile() {
const stats: Stat = await this.fsDriver().stat(this.logFileFullpath());
if (stats.size >= this.maxFileSize) {
const newLogFile: string = this.logFileFullpath(this.getNameToNonActiveLogFile());
await this.fsDriver().move(this.logFileFullpath(), newLogFile);
}
}
private getNameToNonActiveLogFile(): string {
return `log-${Date.now()}.txt`;
}
public async deleteNonActiveLogFiles() {
const files: Stat[] = await this.fsDriver().readDirStats(this.logFilesDir);
for (const file of files) {
if (!file.path.match(/^log-[0-9]+.txt$/gi)) continue;
const timestamp: number = parseInt(file.path.match(/[0-9]+/g)[0], 10);
const ageOfTheFile: number = Date.now() - timestamp;
if (ageOfTheFile >= this.inactiveMaxAge) {
await this.fsDriver().remove(this.logFileFullpath(file.path));
}
}
}
private logFileFullpath(fileName = 'log.txt'): string {
return `${this.logFilesDir}/${fileName}`;
}
private fsDriver() {
return shim.fsDriver();
}
}