From 51a1894902a4277d134722a982370000fe7b4b59 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 28 Feb 2024 15:45:34 -0800 Subject: [PATCH] I guess I needed that logger class --- src/renderer/utils/Logger.js | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/renderer/utils/Logger.js diff --git a/src/renderer/utils/Logger.js b/src/renderer/utils/Logger.js new file mode 100644 index 000000000..da2e8b35c --- /dev/null +++ b/src/renderer/utils/Logger.js @@ -0,0 +1,58 @@ +class Logger { + constructor() { + if (!Logger.instance) { + this.logs = []; + Logger.instance = this; + } + return Logger.instance; + } + + getLogs() { + return this.logs; + } + + log(message, data = null, level = "LOG") { + const timestamp = new Date().toISOString(); + const origin = this.getCallSite(); + const logMessage = `[${timestamp}] [${level}] [${origin}]: ${message}`; + + if (data) { + this.logs.push({ message: logMessage, data }); + console.log(logMessage, data); + } else { + this.logs.push(logMessage); + console.log(logMessage); + } + } + + // Utilize log method for different levels to avoid code duplication + debug(message, data = null) { + this.log(message, data, "DEBUG"); + } + + info(message, data = null) { + this.log(message, data, "INFO"); + } + + warn(message, data = null) { + this.log(message, data, "WARN"); + } + + error(message, data = null) { + this.log(message, data, "ERROR"); + } + + // Helper method to capture the call site + getCallSite() { + const err = new Error(); + Error.captureStackTrace(err, this.getCallSite); + const stack = err.stack.split("\n")[3]; // Adjust this value as needed + // Extract and format call site from stack trace + return stack.trim().replace(/^at\s+/g, ""); + } +} + +const instance = new Logger(); +Object.freeze(instance); + +export default instance;