mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-04-07 14:04:35 +08:00
[feature] Change logger level in Admin Panel
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
},
|
||||
"log": {
|
||||
"filePath": "",
|
||||
"level": "WARN",
|
||||
"options": {
|
||||
"replaceConsole": true
|
||||
}
|
||||
|
||||
@ -9,6 +9,6 @@
|
||||
}
|
||||
},
|
||||
"categories": {
|
||||
"default": {"appenders": ["default"], "level": "ALL"}
|
||||
"default": {"appenders": ["default"]}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +9,6 @@
|
||||
}
|
||||
},
|
||||
"categories": {
|
||||
"default": {"appenders": ["default"], "level": "WARN"}
|
||||
"default": {"appenders": ["default"]}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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');
|
||||
|
||||
|
||||
@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user