mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-04 18:24:46 +02:00
Merge pull request #3405 from mikiher/logger-fixes
Log non-strings into log file like console.log does
This commit is contained in:
commit
703477b157
3 changed files with 437 additions and 172 deletions
|
@ -1,5 +1,6 @@
|
|||
const date = require('./libs/dateAndTime')
|
||||
const { LogLevel } = require('./utils/constants')
|
||||
const util = require('util')
|
||||
|
||||
class Logger {
|
||||
constructor() {
|
||||
|
@ -69,27 +70,29 @@ class Logger {
|
|||
/**
|
||||
*
|
||||
* @param {number} level
|
||||
* @param {string} levelName
|
||||
* @param {string[]} args
|
||||
* @param {string} src
|
||||
*/
|
||||
async handleLog(level, args, src) {
|
||||
async #logToFileAndListeners(level, levelName, args, src) {
|
||||
const expandedArgs = args.map((arg) => (typeof arg !== 'string' ? util.inspect(arg) : arg))
|
||||
const logObj = {
|
||||
timestamp: this.timestamp,
|
||||
source: src,
|
||||
message: args.join(' '),
|
||||
levelName: this.getLogLevelString(level),
|
||||
message: expandedArgs.join(' '),
|
||||
levelName,
|
||||
level
|
||||
}
|
||||
|
||||
// Emit log to sockets that are listening to log events
|
||||
this.socketListeners.forEach((socketListener) => {
|
||||
if (socketListener.level <= level) {
|
||||
if (level >= LogLevel.FATAL || level >= socketListener.level) {
|
||||
socketListener.socket.emit('log', logObj)
|
||||
}
|
||||
})
|
||||
|
||||
// Save log to file
|
||||
if (level >= this.logLevel) {
|
||||
if (level >= LogLevel.FATAL || level >= this.logLevel) {
|
||||
await this.logManager?.logToFile(logObj)
|
||||
}
|
||||
}
|
||||
|
@ -99,50 +102,50 @@ class Logger {
|
|||
this.debug(`Set Log Level to ${this.levelString}`)
|
||||
}
|
||||
|
||||
static ConsoleMethods = {
|
||||
TRACE: 'trace',
|
||||
DEBUG: 'debug',
|
||||
INFO: 'info',
|
||||
WARN: 'warn',
|
||||
ERROR: 'error',
|
||||
FATAL: 'error',
|
||||
NOTE: 'log'
|
||||
}
|
||||
|
||||
#log(levelName, source, ...args) {
|
||||
const level = LogLevel[levelName]
|
||||
if (level < LogLevel.FATAL && level < this.logLevel) return
|
||||
const consoleMethod = Logger.ConsoleMethods[levelName]
|
||||
console[consoleMethod](`[${this.timestamp}] ${levelName}:`, ...args)
|
||||
this.#logToFileAndListeners(level, levelName, args, source)
|
||||
}
|
||||
|
||||
trace(...args) {
|
||||
if (this.logLevel > LogLevel.TRACE) return
|
||||
console.trace(`[${this.timestamp}] TRACE:`, ...args)
|
||||
this.handleLog(LogLevel.TRACE, args, this.source)
|
||||
this.#log('TRACE', this.source, ...args)
|
||||
}
|
||||
|
||||
debug(...args) {
|
||||
if (this.logLevel > LogLevel.DEBUG) return
|
||||
console.debug(`[${this.timestamp}] DEBUG:`, ...args, `(${this.source})`)
|
||||
this.handleLog(LogLevel.DEBUG, args, this.source)
|
||||
this.#log('DEBUG', this.source, ...args)
|
||||
}
|
||||
|
||||
info(...args) {
|
||||
if (this.logLevel > LogLevel.INFO) return
|
||||
console.info(`[${this.timestamp}] INFO:`, ...args)
|
||||
this.handleLog(LogLevel.INFO, args, this.source)
|
||||
this.#log('INFO', this.source, ...args)
|
||||
}
|
||||
|
||||
warn(...args) {
|
||||
if (this.logLevel > LogLevel.WARN) return
|
||||
console.warn(`[${this.timestamp}] WARN:`, ...args, `(${this.source})`)
|
||||
this.handleLog(LogLevel.WARN, args, this.source)
|
||||
this.#log('WARN', this.source, ...args)
|
||||
}
|
||||
|
||||
error(...args) {
|
||||
if (this.logLevel > LogLevel.ERROR) return
|
||||
console.error(`[${this.timestamp}] ERROR:`, ...args, `(${this.source})`)
|
||||
this.handleLog(LogLevel.ERROR, args, this.source)
|
||||
this.#log('ERROR', this.source, ...args)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fatal errors are ones that exit the process
|
||||
* Fatal logs are saved to crash_logs.txt
|
||||
*
|
||||
* @param {...any} args
|
||||
*/
|
||||
fatal(...args) {
|
||||
console.error(`[${this.timestamp}] FATAL:`, ...args, `(${this.source})`)
|
||||
return this.handleLog(LogLevel.FATAL, args, this.source)
|
||||
this.#log('FATAL', this.source, ...args)
|
||||
}
|
||||
|
||||
note(...args) {
|
||||
console.log(`[${this.timestamp}] NOTE:`, ...args)
|
||||
this.handleLog(LogLevel.NOTE, args, this.source)
|
||||
this.#log('NOTE', this.source, ...args)
|
||||
}
|
||||
}
|
||||
module.exports = new Logger()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue