diff --git a/log.d.ts b/log.d.ts new file mode 100644 index 0000000..0bad2e0 --- /dev/null +++ b/log.d.ts @@ -0,0 +1,3 @@ +declare module 'scale-logging'; +// this makes vscode shut up +// no idea if i'll do all this later \ No newline at end of file diff --git a/log.mjs b/log.mjs new file mode 100644 index 0000000..5bc7889 --- /dev/null +++ b/log.mjs @@ -0,0 +1,95 @@ +import chalk from "chalk"; + +class Logging { + + /** + * @param {string} mod Module idenfitier. Used in every line to identify the module that sent the message. + * @param {boolean} silentInst Set to false to log a message when the logger instantiates. Useful for debugging. + * @returns A source for logging messages to the console. Functions for info, warnings, errors, debug statements, and network events are provided and have shorthands. + */ + constructor(mod, silentInst) { + if (!mod) { + this.moduleName = generateRandomString(12); + this.warn(`Logging instantiated without a module identifier, using random string`); + } else if (typeof mod !== 'string') { + this.moduleName = generateRandomString(12); + this.warn(`Logging instantiated with a non-string module identifier, using random string`); + } else { + this.moduleName = mod; + if (silentInst == false) this.info(`Instantiated module logging for ${mod}`); + } + + // logging type shorthand + this.i = this.info; + this.w = this.warn; + this.e = this.error; + this.d = this.debug; + this.n = this.network; + } + + async info(msg) { + let msgFormat = chalk.gray(Logging.getFormattedDate()) + chalk.bgWhite.black(`${this.moduleName} [INFO]`) + chalk.whiteBright(' ' + msg); + console.log(msgFormat); + } + + async warn(msg) { + console.warn(chalk.gray(Logging.getFormattedDate()) + chalk.bgYellow.black(`${this.moduleName} [WARN]`) + chalk.yellowBright(' ' + msg)); + } + + async error(msg) { + console.error(chalk.gray(Logging.getFormattedDate()) + chalk.bgRed.black(`${this.moduleName} [ERROR]`) + chalk.redBright(' ' + msg)); + } + + async debug(msg) { + if (arguments.length !== 1) console.debug(chalk.gray(Logging.getFormattedDate()) + chalk.bgGreen.black(`${this.moduleName} [DEBUG]`) + chalk.greenBright(' ' + coerce(arguments))); + else console.debug(chalk.gray(Logging.getFormattedDate()) + chalk.bgGreen.black(`${this.moduleName} [DEBUG]`) + chalk.greenBright(' ' + msg)); + } + + async network(msg) { + console.log(chalk.gray(Logging.getFormattedDate()) + chalk.bgCyan.black(`${this.moduleName} [NETWORK]`) + chalk.cyanBright(' ' + msg)); + } + + static getFormattedDate() { + const date = new Date(); + + const options = { + month: '2-digit', + day: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: true, + timeZone: process.env.TZ, + timeZoneName: 'short' + }; + + return date.toLocaleString('en-US', options).replace(',', '') + ' '; + } + +} + +function coerce(inArgs) { + let constructedMsg = ""; + let args = Array.from(inArgs); + args.forEach((val) => { + if (typeof val == 'function' || typeof val == 'object') return; + else constructedMsg = constructedMsg + `${val} `; + }); + return constructedMsg; +} + +function generateRandomString(length) { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let randomString = ''; + + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + randomString += characters.charAt(randomIndex); + } + + return randomString; +} + +export { generateRandomString }; +export default Logging; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..1395940 --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "scale-logging", + "version": "1.1.0", + "description": "Organized logging for proxnet-scale", + "type": "module", + "main": "log.mjs", + "author": "@ZombieBrine13092 on GitHub, @zombieb on Discord", + "license": "ISC" +}