From fac5274715de95834089dd8ee7087e9901a0470d Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Wed, 22 May 2024 00:17:06 -0400 Subject: [PATCH] #5 added debugger --- modules/debugger.mjs | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 modules/debugger.mjs diff --git a/modules/debugger.mjs b/modules/debugger.mjs new file mode 100644 index 0000000..8a3f524 --- /dev/null +++ b/modules/debugger.mjs @@ -0,0 +1,67 @@ +// Import necessary modules +import debug from 'debug'; +import { config } from 'dotenv'; +import { writeFile } from 'fs'; +import { inspect } from 'util'; + +// Load environment variables +config(); + +const logLocation = process.env.LOG_LOCATION; + +const writeToLog = async (logMessage, appName) => { + logMessage = `${String(logMessage)}\n`; + + writeFile( + logLocation ?? `./${appName}.log`, + logMessage, + { encoding: "utf-8", flag: 'a+' }, + (err) => { + if (err) console.error(err); + } + ); +}; + +/** + * Create the different logging methods for a function + * Namespace template = ("[app]:[fileName]:['INFO', 'WARNING', 'DEBUG', 'ERROR']") + * @param {string} appName The name of the app to be used in the 'app' portion of the namespace + * @param {string} fileName The name of the file calling the builder to be used in the 'fileName' portion of the namespace + */ +export class DebugBuilder { + constructor(appName, fileName) { + this.INFO = (...messageParts) => { + const _info = debug(`${appName}:${fileName}:INFO`); + _info(messageParts); + writeToLog(`${new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })} - ${appName}:${fileName}:INFO\t-\t${messageParts.map(messagePart => inspect(messagePart))}`, appName); + }; + + this.DEBUG = (...messageParts) => { + const _debug = debug(`${appName}:${fileName}:DEBUG`); + _debug(messageParts); + writeToLog(`${new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })} - ${appName}:${fileName}:DEBUG\t-\t${messageParts.map(messagePart => inspect(messagePart))}`, appName); + }; + + this.VERBOSE = (...messageParts) => { + const _verbose = debug(`${appName}:${fileName}:VERBOSE`); + _verbose(messageParts); + writeToLog(`${new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })} - ${appName}:${fileName}:VERBOSE\t-\t${messageParts.map(messagePart => inspect(messagePart))}`, appName); + }; + + this.WARN = (...messageParts) => { + const _warn = debug(`${appName}:${fileName}:WARNING`); + _warn(messageParts); + writeToLog(`${new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })} - ${appName}:${fileName}:WARNING\t-\t${messageParts.map(messagePart => inspect(messagePart))}`, appName); + }; + + this.ERROR = (...messageParts) => { + const _error = debug(`${appName}:${fileName}:ERROR`); + _error(messageParts); + writeToLog(`${new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })} - ${appName}:${fileName}:ERROR\t-\t${messageParts.map(messagePart => inspect(messagePart))}`, appName); + if (process.env.EXIT_ON_ERROR && process.env.EXIT_ON_ERROR > 0) { + writeToLog("!--- EXITING ---!", appName); + setTimeout(() => process.exit(), process.env.EXIT_ON_ERROR_DELAY ?? 0); + } + }; + } +}