audiobookshelf/server/Logger.js

128 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-07-07 02:18:27 +02:00
const date = require('./libs/dateAndTime')
const { LogLevel } = require('./utils/constants')
2021-08-18 00:01:11 +02:00
class Logger {
constructor() {
this.logLevel = process.env.NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.TRACE
this.socketListeners = []
this.logManager = null
2021-08-18 00:01:11 +02:00
}
get timestamp() {
return date.format(new Date(), 'YYYY-MM-DD HH:mm:ss')
2021-08-18 00:01:11 +02:00
}
get levelString() {
for (const key in LogLevel) {
if (LogLevel[key] === this.logLevel) {
return key
}
}
return 'UNKNOWN'
}
get source() {
try {
throw new Error();
} catch (error) {
return error.stack.split('\n')[3].replace(/^.*\/([^/]*:[0-9]*:[[0-9]*)\)*/, '$1')
}
}
getLogLevelString(level) {
for (const key in LogLevel) {
if (LogLevel[key] === level) {
return key
}
}
return 'UNKNOWN'
}
addSocketListener(socket, level) {
var index = this.socketListeners.findIndex(s => s.id === socket.id)
if (index >= 0) {
this.socketListeners.splice(index, 1, {
id: socket.id,
socket,
level
})
} else {
this.socketListeners.push({
id: socket.id,
socket,
level
})
}
}
removeSocketListener(socketId) {
this.socketListeners = this.socketListeners.filter(s => s.id !== socketId)
}
handleLog(level, args) {
const logObj = {
timestamp: this.timestamp,
source: this.source,
message: args.join(' '),
levelName: this.getLogLevelString(level),
level
}
if (level >= this.logLevel && this.logManager) {
this.logManager.logToFile(logObj)
}
this.socketListeners.forEach((socketListener) => {
if (socketListener.level <= level) {
socketListener.socket.emit('log', logObj)
}
})
}
setLogLevel(level) {
this.logLevel = level
this.debug(`Set Log Level to ${this.levelString}`)
}
2021-08-18 00:01:11 +02:00
trace(...args) {
if (this.logLevel > LogLevel.TRACE) return
console.trace(`[${this.timestamp}[ (${this.source}) TRACE:`, ...args)
this.handleLog(LogLevel.TRACE, args)
2021-08-18 00:01:11 +02:00
}
debug(...args) {
if (this.logLevel > LogLevel.DEBUG) return
console.debug(`[${this.timestamp}[ (${this.source}) DEBUG:`, ...args)
this.handleLog(LogLevel.DEBUG, args)
2021-08-18 00:01:11 +02:00
}
info(...args) {
if (this.logLevel > LogLevel.INFO) return
console.info(`[${this.timestamp}] (${this.source}) INFO:`, ...args)
this.handleLog(LogLevel.INFO, args)
}
2021-08-18 00:01:11 +02:00
warn(...args) {
if (this.logLevel > LogLevel.WARN) return
console.warn(`[${this.timestamp}] (${this.source}) WARN:`, ...args)
this.handleLog(LogLevel.WARN, args)
2021-08-18 00:01:11 +02:00
}
error(...args) {
if (this.logLevel > LogLevel.ERROR) return
console.error(`[${this.timestamp}] (${this.source}) ERROR:`, ...args)
this.handleLog(LogLevel.ERROR, args)
2021-08-18 00:01:11 +02:00
}
fatal(...args) {
console.error(`[${this.timestamp}] (${this.source}) FATAL:`, ...args)
this.handleLog(LogLevel.FATAL, args)
}
note(...args) {
console.log(`[${this.timestamp}] (${this.source}) NOTE:`, ...args)
this.handleLog(LogLevel.NOTE, args)
2021-08-18 00:01:11 +02:00
}
}
module.exports = new Logger()