2022-07-07 02:18:27 +02:00
|
|
|
const date = require('./libs/dateAndTime')
|
2021-10-01 01:52:32 +02:00
|
|
|
const { LogLevel } = require('./utils/constants')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class Logger {
|
|
|
|
constructor() {
|
2021-10-01 01:52:32 +02:00
|
|
|
this.logLevel = process.env.NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.TRACE
|
|
|
|
this.socketListeners = []
|
2021-10-31 23:55:28 +01:00
|
|
|
|
|
|
|
this.logManager = null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get timestamp() {
|
2022-06-18 18:34:28 +02:00
|
|
|
return date.format(new Date(), 'YYYY-MM-DD HH:mm:ss')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
get levelString() {
|
|
|
|
for (const key in LogLevel) {
|
|
|
|
if (LogLevel[key] === this.logLevel) {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'UNKNOWN'
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:06:36 +01:00
|
|
|
get source() {
|
|
|
|
try {
|
|
|
|
throw new Error();
|
|
|
|
} catch (error) {
|
|
|
|
return error.stack.split('\n')[3].replace(/^.*\/([^/]*:[0-9]*:[[0-9]*)\)*/, '$1')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:55:28 +01:00
|
|
|
handleLog(level, args) {
|
|
|
|
const logObj = {
|
|
|
|
timestamp: this.timestamp,
|
2023-01-05 19:06:36 +01:00
|
|
|
source: this.source,
|
2021-10-31 23:55:28 +01:00
|
|
|
message: args.join(' '),
|
|
|
|
levelName: this.getLogLevelString(level),
|
|
|
|
level
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:10:45 +01:00
|
|
|
if (level >= this.logLevel && this.logManager) {
|
2021-10-31 23:55:28 +01:00
|
|
|
this.logManager.logToFile(logObj)
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
this.socketListeners.forEach((socketListener) => {
|
|
|
|
if (socketListener.level <= level) {
|
2021-10-31 23:55:28 +01:00
|
|
|
socketListener.socket.emit('log', logObj)
|
2021-10-01 01:52:32 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setLogLevel(level) {
|
|
|
|
this.logLevel = level
|
|
|
|
this.debug(`Set Log Level to ${this.levelString}`)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
trace(...args) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel > LogLevel.TRACE) return
|
2023-01-05 19:06:36 +01:00
|
|
|
console.trace(`[${this.timestamp}[ (${this.source}) TRACE:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.TRACE, args)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug(...args) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel > LogLevel.DEBUG) return
|
2023-01-05 19:06:36 +01:00
|
|
|
console.debug(`[${this.timestamp}[ (${this.source}) DEBUG:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.DEBUG, args)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
info(...args) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel > LogLevel.INFO) return
|
2023-01-05 19:06:36 +01:00
|
|
|
console.info(`[${this.timestamp}] (${this.source}) INFO:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.INFO, args)
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
warn(...args) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel > LogLevel.WARN) return
|
2023-01-05 19:06:36 +01:00
|
|
|
console.warn(`[${this.timestamp}] (${this.source}) WARN:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.WARN, args)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
error(...args) {
|
2021-10-01 01:52:32 +02:00
|
|
|
if (this.logLevel > LogLevel.ERROR) return
|
2023-01-05 19:06:36 +01:00
|
|
|
console.error(`[${this.timestamp}] (${this.source}) ERROR:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.ERROR, args)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fatal(...args) {
|
2023-01-05 19:06:36 +01:00
|
|
|
console.error(`[${this.timestamp}] (${this.source}) FATAL:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.FATAL, args)
|
2021-10-01 01:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
note(...args) {
|
2023-01-05 19:06:36 +01:00
|
|
|
console.log(`[${this.timestamp}] (${this.source}) NOTE:`, ...args)
|
2021-10-31 23:55:28 +01:00
|
|
|
this.handleLog(LogLevel.NOTE, args)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new Logger()
|