From 9988696df26aba9c34733f9093ebf3d957ba9d02 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 23 Dec 2022 14:44:43 +0100 Subject: [PATCH] Add configuration directive for the maximum log files size MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/types/ConfigurationData.ts | 3 ++- src/utils/Configuration.ts | 16 ++++++++++++---- src/utils/Logger.ts | 7 +++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/types/ConfigurationData.ts b/src/types/ConfigurationData.ts index 58372a38..8ab9bcfd 100644 --- a/src/types/ConfigurationData.ts +++ b/src/types/ConfigurationData.ts @@ -73,7 +73,8 @@ export type ConfigurationData = { logFormat?: string; logLevel?: string; logRotate?: boolean; - logMaxFiles?: number; + logMaxFiles?: number | string; + logMaxSize?: number | string; logFile?: string; logErrorFile?: string; logConsole?: boolean; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index f1893531..c17cad01 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -246,10 +246,18 @@ export default class Configuration { : true; } - static getLogMaxFiles(): number { - return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') - ? Configuration.getConfig().logMaxFiles - : 7; + static getLogMaxFiles(): number | string | undefined { + return ( + Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Configuration.getConfig().logMaxFiles + ); + } + + static getLogMaxSize(): number | string | undefined { + return ( + Configuration.objectHasOwnProperty(Configuration.getConfig(), 'logMaxFiles') && + Configuration.getConfig().logMaxSize + ); } static getLogLevel(): string { diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index 95d5347d..61e4b0bc 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -9,6 +9,7 @@ import Utils from './Utils'; let transports: transport[]; if (Configuration.getLogRotate()) { const logMaxFiles = Configuration.getLogMaxFiles(); + const logMaxSize = Configuration.getLogMaxSize(); transports = [ new DailyRotateFile({ filename: Utils.insertAt( @@ -17,7 +18,8 @@ if (Configuration.getLogRotate()) { Configuration.getLogErrorFile().indexOf('.log') ), level: 'error', - maxFiles: logMaxFiles, + ...(logMaxFiles && { maxFiles: logMaxFiles }), + ...(logMaxSize && { maxSize: logMaxSize }), }), new DailyRotateFile({ filename: Utils.insertAt( @@ -25,7 +27,8 @@ if (Configuration.getLogRotate()) { '-%DATE%', Configuration.getLogFile().indexOf('.log') ), - maxFiles: logMaxFiles, + ...(logMaxFiles && { maxFiles: logMaxFiles }), + ...(logMaxSize && { maxSize: logMaxSize }), }), ]; } else { -- 2.34.1