feat(log): read from config.ini instead of options

This commit is contained in:
Elian Doran 2025-08-13 15:10:19 +03:00
parent 7a036fc777
commit 43fd0924a1
No known key found for this signature in database
3 changed files with 47 additions and 20 deletions

View File

@ -3,7 +3,7 @@ import fs from "fs";
import dataDir from "./data_dir.js"; import dataDir from "./data_dir.js";
import path from "path"; import path from "path";
import resourceDir from "./resource_dir.js"; import resourceDir from "./resource_dir.js";
import { envToBoolean } from "./utils.js"; import { envToBoolean, stringToInt } from "./utils.js";
const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini"); const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini");
@ -50,7 +50,15 @@ export interface TriliumConfig {
oauthIssuerName: string; oauthIssuerName: string;
oauthIssuerIcon: string; oauthIssuerIcon: string;
}; };
Logging: {
/**
* The number of days to keep the log files around. When rotating the logs, log files created by Trilium older than the specified amount of time will be deleted.
*/
retentionDays: number;
} }
}
export const LOGGING_DEFAULT_RETENTION_DAYS = 90;
//prettier-ignore //prettier-ignore
const config: TriliumConfig = { const config: TriliumConfig = {
@ -136,6 +144,13 @@ const config: TriliumConfig = {
oauthIssuerIcon: oauthIssuerIcon:
process.env.TRILIUM_OAUTH_ISSUER_ICON || iniConfig?.MultiFactorAuthentication?.oauthIssuerIcon || "" process.env.TRILIUM_OAUTH_ISSUER_ICON || iniConfig?.MultiFactorAuthentication?.oauthIssuerIcon || ""
},
Logging: {
retentionDays:
stringToInt(process.env.TRILIUM_LOGGING_RETENTION_DAYS) ??
stringToInt(iniConfig?.Logging?.retentionDays) ??
LOGGING_DEFAULT_RETENTION_DAYS
} }
}; };

View File

@ -6,6 +6,7 @@ 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 config, { LOGGING_DEFAULT_RETENTION_DAYS } from "./config.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);
@ -18,7 +19,6 @@ const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE; const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR; const DAY = 24 * HOUR;
const DEFAULT_RETENTION_DAYS = 90;
const MINIMUM_FILES_TO_KEEP = 7; const MINIMUM_FILES_TO_KEEP = 7;
let todaysMidnight!: Date; let todaysMidnight!: Date;
@ -34,17 +34,10 @@ function getTodaysMidnight() {
async function cleanupOldLogFiles() { async function cleanupOldLogFiles() {
try { try {
// Get retention days from environment or options // Get retention days from environment or options
const envRetention = process.env.TRILIUM_LOG_RETENTION_DAYS; let retentionDays = LOGGING_DEFAULT_RETENTION_DAYS;
let retentionDays = DEFAULT_RETENTION_DAYS; const customRetentionDays = config.Logging.retentionDays;
if (customRetentionDays > 0) {
if (envRetention) { retentionDays = customRetentionDays;
const parsed = parseInt(envRetention, 10);
if (!isNaN(parsed) && parsed > 0 && parsed <= 3650) {
retentionDays = parsed;
}
} else {
const optionService = (await import("./options.js")).default;
retentionDays = optionService.getOptionInt("logRetentionDays", DEFAULT_RETENTION_DAYS);
} }
const cutoffDate = new Date(); const cutoffDate = new Date();

View File

@ -281,6 +281,25 @@ export function envToBoolean(val: string | undefined) {
return undefined; return undefined;
} }
/**
* Parses a string value to an integer. If the resulting number is NaN or undefined, the result is also undefined.
*
* @param val the value to parse.
* @returns the parsed value.
*/
export function stringToInt(val: string | undefined) {
if (!val) {
return undefined;
}
const parsed = parseInt(val, 10);
if (Number.isNaN(parsed)) {
return undefined;
}
return parsed;
}
/** /**
* Returns the directory for resources. On Electron builds this corresponds to the `resources` subdirectory inside the distributable package. * Returns the directory for resources. On Electron builds this corresponds to the `resources` subdirectory inside the distributable package.
* On development builds, this simply refers to the src directory of the application. * On development builds, this simply refers to the src directory of the application.