mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +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);
|
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
||||||
}
|
}
|
||||||
|
|
||||||
let logFile!: fs.WriteStream;
|
let logFile: fs.WriteStream | undefined;
|
||||||
|
|
||||||
const SECOND = 1000;
|
const SECOND = 1000;
|
||||||
const MINUTE = 60 * SECOND;
|
const MINUTE = 60 * SECOND;
|
||||||
@@ -107,17 +107,20 @@ function initLogFile() {
|
|||||||
todaysMidnight = getTodaysMidnight();
|
todaysMidnight = getTodaysMidnight();
|
||||||
|
|
||||||
const logPath = `${dataDir.LOG_DIR}/trilium-${formatDate()}.log`;
|
const logPath = `${dataDir.LOG_DIR}/trilium-${formatDate()}.log`;
|
||||||
|
const isRotating = !!logFile;
|
||||||
|
|
||||||
if (logFile) {
|
if (isRotating) {
|
||||||
logFile.end();
|
logFile!.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile = fs.createWriteStream(logPath, { flags: "a" });
|
||||||
|
|
||||||
// Clean up old log files when rotating to a new file
|
// Clean up old log files when rotating to a new file
|
||||||
|
if (isRotating) {
|
||||||
cleanupOldLogFiles().catch(() => {
|
cleanupOldLogFiles().catch(() => {
|
||||||
// Ignore cleanup errors
|
// Ignore cleanup errors
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logFile = fs.createWriteStream(logPath, { flags: "a" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkDate(millisSinceMidnight: number) {
|
function checkDate(millisSinceMidnight: number) {
|
||||||
@@ -141,7 +144,7 @@ function log(str: string | Error) {
|
|||||||
|
|
||||||
millisSinceMidnight = checkDate(millisSinceMidnight);
|
millisSinceMidnight = checkDate(millisSinceMidnight);
|
||||||
|
|
||||||
logFile.write(`${formatTime(millisSinceMidnight)} ${str}${EOL}`);
|
logFile!.write(`${formatTime(millisSinceMidnight)} ${str}${EOL}`);
|
||||||
|
|
||||||
console.log(str);
|
console.log(str);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user