mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	fix(server): potential race condition when rotating logs
This commit is contained in:
		
							parent
							
								
									91f3bc4488
								
							
						
					
					
						commit
						d59993abf6
					
				@ -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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user