audiobookshelf/server/Logger.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-08-18 00:01:11 +02:00
const LOG_LEVEL = {
TRACE: 0,
DEBUG: 1,
INFO: 2,
WARN: 3,
ERROR: 4,
FATAL: 5
}
class Logger {
constructor() {
let env_log_level = process.env.LOG_LEVEL || 'TRACE'
this.LogLevel = LOG_LEVEL[env_log_level] || LOG_LEVEL.TRACE
this.info(`Log Level: ${this.LogLevel}`)
}
get timestamp() {
return (new Date()).toISOString()
}
trace(...args) {
if (this.LogLevel > LOG_LEVEL.TRACE) return
console.trace(`[${this.timestamp}] TRACE:`, ...args)
}
debug(...args) {
if (this.LogLevel > LOG_LEVEL.DEBUG) return
console.debug(`[${this.timestamp}] DEBUG:`, ...args)
}
info(...args) {
if (this.LogLevel > LOG_LEVEL.INFO) return
console.info(`[${this.timestamp}] INFO:`, ...args)
}
warn(...args) {
if (this.LogLevel > LOG_LEVEL.WARN) return
console.warn(`[${this.timestamp}] WARN:`, ...args)
}
error(...args) {
if (this.LogLevel > LOG_LEVEL.ERROR) return
console.error(`[${this.timestamp}] ERROR:`, ...args)
}
fatal(...args) {
console.error(`[${this.timestamp}] FATAL:`, ...args)
}
}
module.exports = new Logger()