Read log level from configuration and update readme

- Break out reading of settings in js and remove it from shell scripts
- Pick up config from Logger via Configuration class
This commit is contained in:
Victor Hagelbäck 2021-01-21 23:40:59 +01:00
parent 4176cfb714
commit 1b55cabf63
7 changed files with 90 additions and 34 deletions

View file

@ -1,6 +1,10 @@
const winston = require("winston");
const { colorize, combine, label, printf, timestamp } = winston.format;
const Configuration = require("./Configuration");
const LEVELS = ["error", "warn", "info", "debug", "verbose", "silly"];
const logFormat = printf(info => {
if(info.stack) {
return `${info.timestamp} ${info.level} [${info.label}] ${info.message}\n${info.stack}`;
@ -19,11 +23,17 @@ class Logger {
* - swap debug/verbose levels and omit http to mimic HA standard
* Levels (in order): error, warn, info, debug, verbose, silly
* */
static getLogger(moduleName, level="verbose") {
static getLogger(moduleName) {
const config = Configuration.getConfiguration();
const level = (config.logLevel && LEVELS.find(l => l.startsWith(config.logLevel[0].toLowerCase()))) || "info";
const logger = winston.createLogger({
format: combine(
winston.format(info => {
switch (info.level) {
case "warning":
info.level = "WRN";
break;
case "verbose":
info.level = "VRB";
break;
@ -48,6 +58,12 @@ class Logger {
]
});
winston.addColors(Logger.logLevels().colors);
if (moduleName === "plejd-main") {
logger.log(level, `Log level set to ${level}`);
}
return logger;
}