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`
This commit is contained in:
Benjamin Porter 2023-12-01 17:59:46 -07:00
parent 8c6a2ac5dd
commit b1dd88f33f
2 changed files with 34 additions and 4 deletions

View file

@ -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()
module.exports = new Logger()

View file

@ -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'
}
}