From b1dd88f33f1da3fd7fe2a1d933036ad95a00f884 Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Fri, 1 Dec 2023 17:59:46 -0700 Subject: [PATCH] Allow user to override log level This PR adds an environment variable `LOG_LEVEL` that can be used to control the application log level. For example, this would allow you to view dev logs while in production, should you so choose. If the env var is not set, behavior should not change from the current behavior. Env var can be set with the log level string (case insensitive). For example: `LOG_LEVEL=debug` --- server/Logger.js | 13 +++++++++++-- server/utils/constants.js | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/Logger.js b/server/Logger.js index b49531893..0c498ec1f 100644 --- a/server/Logger.js +++ b/server/Logger.js @@ -5,7 +5,16 @@ class Logger { constructor() { this.isDev = process.env.NODE_ENV !== 'production' this.logLevel = !this.isDev ? LogLevel.INFO : LogLevel.TRACE - this.hideDevLogs = process.env.HIDE_DEV_LOGS === undefined ? !this.isDev : process.env.HIDE_DEV_LOGS === '1' + + // Allow the user to override the log level if desired + if (process.env.LOG_LEVEL) { + this.logLevel = parseInt(LogLevel.strToLevel(process.env.LOG_LEVEL)) + } else { + this.hideDevLogs = process.env.HIDE_DEV_LOGS === undefined + ? !this.isDev + : process.env.HIDE_DEV_LOGS === '1' + } + this.socketListeners = [] this.logManager = null @@ -137,4 +146,4 @@ class Logger { this.handleLog(LogLevel.NOTE, args) } } -module.exports = new Logger() \ No newline at end of file +module.exports = new Logger() diff --git a/server/utils/constants.js b/server/utils/constants.js index 7a21d2ddc..193172f6e 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -23,7 +23,28 @@ module.exports.LogLevel = { WARN: 3, ERROR: 4, FATAL: 5, - NOTE: 6 + NOTE: 6, + + strToLevel(str) { + switch (str.toUpperCase()) { + case 'TRACE': + return this.TRACE + case 'DEBUG': + return this.DEBUG + case 'INFO': + return this.INFO + case 'WARN': + return this.WARN + case 'ERROR': + return this.ERROR + case 'FATAL': + return this.FATAL + case 'NOTE': + return this.NOTE + default: + return this.INFO + } + } } module.exports.PlayMethod = { @@ -54,4 +75,4 @@ module.exports.AudioMimeType = { module.exports.VideoMimeType = { MP4: 'video/mp4' -} \ No newline at end of file +}