From 0f524814647c98837382fe995f162b7b06173be4 Mon Sep 17 00:00:00 2001 From: PauI Ostrovckij Date: Tue, 30 Sep 2025 10:25:26 +0300 Subject: [PATCH] [feature] Change logger level in Admin Panel --- Common/config/default.json | 1 + Common/config/log4js/development.json | 2 +- Common/config/log4js/production.json | 2 +- Common/config/schemas/config.schema.json | 12 ++++++++++++ Common/sources/logger.js | 18 +++++++++++++++++- Common/sources/runtimeConfigManager.js | 8 ++++++++ 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Common/config/default.json b/Common/config/default.json index 18f10551..22d4db2b 100644 --- a/Common/config/default.json +++ b/Common/config/default.json @@ -21,6 +21,7 @@ }, "log": { "filePath": "", + "level": "WARN", "options": { "replaceConsole": true } diff --git a/Common/config/log4js/development.json b/Common/config/log4js/development.json index ff1d0326..ba2748e9 100644 --- a/Common/config/log4js/development.json +++ b/Common/config/log4js/development.json @@ -9,6 +9,6 @@ } }, "categories": { - "default": {"appenders": ["default"], "level": "ALL"} + "default": {"appenders": ["default"]} } } diff --git a/Common/config/log4js/production.json b/Common/config/log4js/production.json index 5b6673e9..df2a60bc 100644 --- a/Common/config/log4js/production.json +++ b/Common/config/log4js/production.json @@ -9,6 +9,6 @@ } }, "categories": { - "default": {"appenders": ["default"], "level": "WARN"} + "default": {"appenders": ["default"]} } } diff --git a/Common/config/schemas/config.schema.json b/Common/config/schemas/config.schema.json index e14a2581..c748899f 100644 --- a/Common/config/schemas/config.schema.json +++ b/Common/config/schemas/config.schema.json @@ -214,6 +214,18 @@ } } } + }, + "log": { + "type": "object", + "additionalProperties": true, + "x-scope": "admin", + "properties": { + "level": { + "type": "string", + "enum": ["ALL", "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF"], + "description": "Logging level for the application" + } + } } } } diff --git a/Common/sources/logger.js b/Common/sources/logger.js index 0aa6066d..71ee37bd 100644 --- a/Common/sources/logger.js +++ b/Common/sources/logger.js @@ -34,9 +34,14 @@ const config = require('config'); const util = require('util'); +const path = require('path'); +const fs = require('fs'); const log4js = require('log4js'); const layouts = require('log4js/lib/layouts'); +const logConfigPath = config.get('log.filePath'); +let logLevel = config.get('log.level'); +const fullLogConfigPath = path.resolve(__dirname, '..', logConfigPath); // https://stackoverflow.com/a/36643588 const dateToJSONWithTZ = function (d) { @@ -78,7 +83,18 @@ log4js.addLayout('patternWithTokens', cfg => { return layouts.patternLayout(pattern, tokens); }); -log4js.configure(config.get('log.filePath')); +exports.configureLogger = function (level = logLevel) { + const logConfig = JSON.parse(fs.readFileSync(fullLogConfigPath, 'utf8')); + Object.keys(logConfig.categories).forEach(categoryName => { + logConfig.categories[categoryName].level = level; + }); + logLevel = level; + log4js.configure(logConfig); +}; +exports.configureLogger(); +exports.getLogLevel = function () { + return logLevel; +}; const logger = log4js.getLogger('nodeJS'); diff --git a/Common/sources/runtimeConfigManager.js b/Common/sources/runtimeConfigManager.js index 3fbab251..07953b61 100644 --- a/Common/sources/runtimeConfigManager.js +++ b/Common/sources/runtimeConfigManager.js @@ -38,6 +38,7 @@ const config = require('config'); const NodeCache = require('node-cache'); const operationContext = require('./operationContext'); const utils = require('./utils'); +const logger = require('./logger'); const cfgRuntimeConfig = config.get('runtimeConfig'); const configFilePath = cfgRuntimeConfig.filePath; @@ -102,6 +103,7 @@ async function saveConfig(ctx, config) { */ function handleConfigFileChange(eventTypeOrCurrent, filenameOrPrevious) { try { + const logLevel = logger.getLogLevel(); let shouldReload = false; if (typeof eventTypeOrCurrent === 'object' && eventTypeOrCurrent.isFile) { @@ -113,6 +115,12 @@ function handleConfigFileChange(eventTypeOrCurrent, filenameOrPrevious) { } if (shouldReload) { nodeCache.del(configFileName); + // Reload config and update logging level + getConfig(operationContext.global).then(config => { + if (config.log.level !== logLevel) { + logger.configureLogger(config.log.level); + } + }); } } catch (err) { operationContext.global.logger.error(`handleConfigFileChange error: ${err.message}`);