mirror of
https://github.com/zadam/trilium.git
synced 2025-10-26 07:46:30 +01:00
fix(server): potential race condition when rotating logs
This commit is contained in:
@@ -12,7 +12,7 @@ if (!fs.existsSync(dataDir.LOG_DIR)) {
|
||||
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
||||
}
|
||||
|
||||
let logFile!: fs.WriteStream;
|
||||
let logFile: fs.WriteStream | undefined;
|
||||
|
||||
const SECOND = 1000;
|
||||
const MINUTE = 60 * SECOND;
|
||||
@@ -107,17 +107,20 @@ function initLogFile() {
|
||||
todaysMidnight = getTodaysMidnight();
|
||||
|
||||
const logPath = `${dataDir.LOG_DIR}/trilium-${formatDate()}.log`;
|
||||
const isRotating = !!logFile;
|
||||
|
||||
if (logFile) {
|
||||
logFile.end();
|
||||
if (isRotating) {
|
||||
logFile!.end();
|
||||
}
|
||||
|
||||
// Clean up old log files when rotating to a new file
|
||||
logFile = fs.createWriteStream(logPath, { flags: "a" });
|
||||
|
||||
// Clean up old log files when rotating to a new file
|
||||
if (isRotating) {
|
||||
cleanupOldLogFiles().catch(() => {
|
||||
// Ignore cleanup errors
|
||||
});
|
||||
}
|
||||
|
||||
logFile = fs.createWriteStream(logPath, { flags: "a" });
|
||||
}
|
||||
|
||||
function checkDate(millisSinceMidnight: number) {
|
||||
@@ -141,7 +144,7 @@ function log(str: string | Error) {
|
||||
|
||||
millisSinceMidnight = checkDate(millisSinceMidnight);
|
||||
|
||||
logFile.write(`${formatTime(millisSinceMidnight)} ${str}${EOL}`);
|
||||
logFile!.write(`${formatTime(millisSinceMidnight)} ${str}${EOL}`);
|
||||
|
||||
console.log(str);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user