mirror of
https://github.com/zadam/trilium.git
synced 2025-10-21 15:49:00 +02:00
fix(log): cyclic dependency to options breaking tests
This commit is contained in:
parent
54efa6b38c
commit
7a036fc777
@ -6,7 +6,6 @@ import path from "path";
|
|||||||
import { EOL } from "os";
|
import { EOL } from "os";
|
||||||
import dataDir from "./data_dir.js";
|
import dataDir from "./data_dir.js";
|
||||||
import cls from "./cls.js";
|
import cls from "./cls.js";
|
||||||
import optionService from "./options.js";
|
|
||||||
|
|
||||||
if (!fs.existsSync(dataDir.LOG_DIR)) {
|
if (!fs.existsSync(dataDir.LOG_DIR)) {
|
||||||
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
||||||
@ -37,38 +36,39 @@ async function cleanupOldLogFiles() {
|
|||||||
// Get retention days from environment or options
|
// Get retention days from environment or options
|
||||||
const envRetention = process.env.TRILIUM_LOG_RETENTION_DAYS;
|
const envRetention = process.env.TRILIUM_LOG_RETENTION_DAYS;
|
||||||
let retentionDays = DEFAULT_RETENTION_DAYS;
|
let retentionDays = DEFAULT_RETENTION_DAYS;
|
||||||
|
|
||||||
if (envRetention) {
|
if (envRetention) {
|
||||||
const parsed = parseInt(envRetention, 10);
|
const parsed = parseInt(envRetention, 10);
|
||||||
if (!isNaN(parsed) && parsed > 0 && parsed <= 3650) {
|
if (!isNaN(parsed) && parsed > 0 && parsed <= 3650) {
|
||||||
retentionDays = parsed;
|
retentionDays = parsed;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
const optionService = (await import("./options.js")).default;
|
||||||
retentionDays = optionService.getOptionInt("logRetentionDays", DEFAULT_RETENTION_DAYS);
|
retentionDays = optionService.getOptionInt("logRetentionDays", DEFAULT_RETENTION_DAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
const cutoffDate = new Date();
|
const cutoffDate = new Date();
|
||||||
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
|
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
|
||||||
|
|
||||||
// Read all log files
|
// Read all log files
|
||||||
const files = await fs.promises.readdir(dataDir.LOG_DIR);
|
const files = await fs.promises.readdir(dataDir.LOG_DIR);
|
||||||
const logFiles: Array<{name: string, mtime: Date, path: string}> = [];
|
const logFiles: Array<{name: string, mtime: Date, path: string}> = [];
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
// Security: Only process files matching our log pattern
|
// Security: Only process files matching our log pattern
|
||||||
if (!/^trilium-\d{4}-\d{2}-\d{2}\.log$/.test(file)) {
|
if (!/^trilium-\d{4}-\d{2}-\d{2}\.log$/.test(file)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filePath = path.join(dataDir.LOG_DIR, file);
|
const filePath = path.join(dataDir.LOG_DIR, file);
|
||||||
|
|
||||||
// Security: Verify path stays within LOG_DIR
|
// Security: Verify path stays within LOG_DIR
|
||||||
const resolvedPath = path.resolve(filePath);
|
const resolvedPath = path.resolve(filePath);
|
||||||
const resolvedLogDir = path.resolve(dataDir.LOG_DIR);
|
const resolvedLogDir = path.resolve(dataDir.LOG_DIR);
|
||||||
if (!resolvedPath.startsWith(resolvedLogDir + path.sep)) {
|
if (!resolvedPath.startsWith(resolvedLogDir + path.sep)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stats = await fs.promises.stat(filePath);
|
const stats = await fs.promises.stat(filePath);
|
||||||
logFiles.push({ name: file, mtime: stats.mtime, path: filePath });
|
logFiles.push({ name: file, mtime: stats.mtime, path: filePath });
|
||||||
@ -76,15 +76,15 @@ async function cleanupOldLogFiles() {
|
|||||||
// Skip files we can't stat
|
// Skip files we can't stat
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by modification time (oldest first)
|
// Sort by modification time (oldest first)
|
||||||
logFiles.sort((a, b) => a.mtime.getTime() - b.mtime.getTime());
|
logFiles.sort((a, b) => a.mtime.getTime() - b.mtime.getTime());
|
||||||
|
|
||||||
// Keep minimum number of files
|
// Keep minimum number of files
|
||||||
if (logFiles.length <= MINIMUM_FILES_TO_KEEP) {
|
if (logFiles.length <= MINIMUM_FILES_TO_KEEP) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old files, keeping minimum
|
// Delete old files, keeping minimum
|
||||||
let deletedCount = 0;
|
let deletedCount = 0;
|
||||||
for (let i = 0; i < logFiles.length - MINIMUM_FILES_TO_KEEP; i++) {
|
for (let i = 0; i < logFiles.length - MINIMUM_FILES_TO_KEEP; i++) {
|
||||||
@ -98,7 +98,7 @@ async function cleanupOldLogFiles() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deletedCount > 0) {
|
if (deletedCount > 0) {
|
||||||
info(`Log cleanup: deleted ${deletedCount} old log files`);
|
info(`Log cleanup: deleted ${deletedCount} old log files`);
|
||||||
}
|
}
|
||||||
@ -114,7 +114,7 @@ function initLogFile() {
|
|||||||
|
|
||||||
if (logFile) {
|
if (logFile) {
|
||||||
logFile.end();
|
logFile.end();
|
||||||
|
|
||||||
// Clean up old log files when rotating to a new file
|
// Clean up old log files when rotating to a new file
|
||||||
cleanupOldLogFiles().catch(() => {
|
cleanupOldLogFiles().catch(() => {
|
||||||
// Ignore cleanup errors
|
// Ignore cleanup errors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user