Skip to content

Commit 7a036fc

Browse files
committed
fix(log): cyclic dependency to options breaking tests
1 parent 54efa6b commit 7a036fc

File tree

1 file changed

+13
-13
lines changed
  • apps/server/src/services

1 file changed

+13
-13
lines changed

apps/server/src/services/log.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import path from "path";
66
import { EOL } from "os";
77
import dataDir from "./data_dir.js";
88
import cls from "./cls.js";
9-
import optionService from "./options.js";
109

1110
if (!fs.existsSync(dataDir.LOG_DIR)) {
1211
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
@@ -37,54 +36,55 @@ async function cleanupOldLogFiles() {
3736
// Get retention days from environment or options
3837
const envRetention = process.env.TRILIUM_LOG_RETENTION_DAYS;
3938
let retentionDays = DEFAULT_RETENTION_DAYS;
40-
39+
4140
if (envRetention) {
4241
const parsed = parseInt(envRetention, 10);
4342
if (!isNaN(parsed) && parsed > 0 && parsed <= 3650) {
4443
retentionDays = parsed;
4544
}
4645
} else {
46+
const optionService = (await import("./options.js")).default;
4747
retentionDays = optionService.getOptionInt("logRetentionDays", DEFAULT_RETENTION_DAYS);
4848
}
49-
49+
5050
const cutoffDate = new Date();
5151
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
52-
52+
5353
// Read all log files
5454
const files = await fs.promises.readdir(dataDir.LOG_DIR);
5555
const logFiles: Array<{name: string, mtime: Date, path: string}> = [];
56-
56+
5757
for (const file of files) {
5858
// Security: Only process files matching our log pattern
5959
if (!/^trilium-\d{4}-\d{2}-\d{2}\.log$/.test(file)) {
6060
continue;
6161
}
62-
62+
6363
const filePath = path.join(dataDir.LOG_DIR, file);
64-
64+
6565
// Security: Verify path stays within LOG_DIR
6666
const resolvedPath = path.resolve(filePath);
6767
const resolvedLogDir = path.resolve(dataDir.LOG_DIR);
6868
if (!resolvedPath.startsWith(resolvedLogDir + path.sep)) {
6969
continue;
7070
}
71-
71+
7272
try {
7373
const stats = await fs.promises.stat(filePath);
7474
logFiles.push({ name: file, mtime: stats.mtime, path: filePath });
7575
} catch (err) {
7676
// Skip files we can't stat
7777
}
7878
}
79-
79+
8080
// Sort by modification time (oldest first)
8181
logFiles.sort((a, b) => a.mtime.getTime() - b.mtime.getTime());
82-
82+
8383
// Keep minimum number of files
8484
if (logFiles.length <= MINIMUM_FILES_TO_KEEP) {
8585
return;
8686
}
87-
87+
8888
// Delete old files, keeping minimum
8989
let deletedCount = 0;
9090
for (let i = 0; i < logFiles.length - MINIMUM_FILES_TO_KEEP; i++) {
@@ -98,7 +98,7 @@ async function cleanupOldLogFiles() {
9898
}
9999
}
100100
}
101-
101+
102102
if (deletedCount > 0) {
103103
info(`Log cleanup: deleted ${deletedCount} old log files`);
104104
}
@@ -114,7 +114,7 @@ function initLogFile() {
114114

115115
if (logFile) {
116116
logFile.end();
117-
117+
118118
// Clean up old log files when rotating to a new file
119119
cleanupOldLogFiles().catch(() => {
120120
// Ignore cleanup errors

0 commit comments

Comments
 (0)