Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log non-strings into log file like console.log does #3405

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions server/Logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const date = require('./libs/dateAndTime')
const { LogLevel } = require('./utils/constants')
const util = require('util')

class Logger {
constructor() {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Loading