From dd882eb048413bb37896c4d6f4d1420ecc6e16d8 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 16 Dec 2022 04:58:49 -0500 Subject: [PATCH] Make logs more readable --- index.js | 13 +++--- utils/logging.js | 62 ++++++++++++++++++++++++++ pm2-metrics.js => utils/pm2-metrics.js | 5 +++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 utils/logging.js rename pm2-metrics.js => utils/pm2-metrics.js (82%) diff --git a/index.js b/index.js index 5b9d6d4..f087e32 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,10 @@ const fs = require('node:fs'); const path = require('node:path'); // Add pm2 metrics - this should NEVER track ANYTHING identifiable. This is purely for basic metrics and bot performance tracking -const metrics = require('./pm2-metrics.js'); +const metrics = require('./utils/pm2-metrics.js'); + +// Use a custom logging script +const logger = require('./utils/logging.js'); // Require the necessary discord.js classes const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); @@ -31,14 +34,14 @@ for (const file of commandFiles) { client.commands.set(command.data.name, command); } else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + logger.log(logger.logLevels.WARN, `The command at ${filePath} is missing a required "data" or "execute" property.`); } } // When the client is ready, run this code (only once) // We use 'c' for the event parameter to keep it separate from the already defined 'client' client.once(Events.ClientReady, c => { - console.log(`Ready! Logged in as ${c.user.tag}`); + logger.log(logger.logLevels.INFO, `${logger.colorText('Ready!', logger.textColor.Green)} Logged in as ${logger.colorText(c.user.tag, logger.textColor.Blue)}`); }); // Client "on" Events @@ -48,7 +51,7 @@ client.on(Events.InteractionCreate, async interaction => { const command = interaction.client.commands.get(interaction.commandName); if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); + logger.log(logger.logLevels.ERROR, `No command matching ${interaction.commandName} was found.`); await interaction.reply({ content: `This command no longer exists! Please contact ${botOwner} to report that this is happening!`, ephemeral: true }); // Report error to PM2 dashboard @@ -66,7 +69,7 @@ client.on(Events.InteractionCreate, async interaction => { await command.execute(interaction); } catch (error) { - console.error(error); + logger.log(logger.logLevels.ERROR, error); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); // Report error to PM2 dashboard diff --git a/utils/logging.js b/utils/logging.js new file mode 100644 index 0000000..002f0b0 --- /dev/null +++ b/utils/logging.js @@ -0,0 +1,62 @@ +/* +* Konpeki Shiho - Utility Definition File +* logging.js - A custom logging script to apply information to output +* +* Though console.error() and console.warn() exist, they don't exactly fit what is wanted here. +*/ + +// Enum list of severity levels +const logLevels = { + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, +}; + +// Enum list of text colors +const textColor = { + White: '\x1b[97m', + Gray: '\x1b[37m', + Yellow: '\x1b[33m', + Red: '\x1b[91m', + Blue: '\x1b[96m', + Green: '\x1b[92m', + Reset: '\x1b[0m', +}; + +// Get the current Date and Time +const date_time = new Date(new Date().toUTCString()); +const datetime = '[' + ('0' + (date_time.getMonth() + 1)).slice(-2) + '/' + ('0' + date_time.getDate()).slice(-2) + '/' + date_time.getFullYear() + ' ' + ('0' + (date_time.getHours() + 1)).slice(-2) + ':' + ('0' + (date_time.getMinutes() + 1)).slice(-2) + ':' + ('0' + (date_time.getSeconds() + 1)).slice(-2) + ' UTC]'; + +// Add color to the text passed through - If a text manip util is made, move this there +const colorText = function(message, color) { + return color + message + textColor.Reset; +}; + +// Print log message to console based on severity level +const log = function(logLevel, message) { + switch (logLevel) { + case 0: + console.log(datetime + ' ' + colorText('[DEBUG]', textColor.Gray) + ' ' + message); + break; + case 1: + console.log(datetime + ' ' + colorText('[INFO]', textColor.White) + ' ' + message); + break; + case 2: + console.log(datetime + ' ' + colorText('[WARN]', textColor.Yellow) + ' ' + message); + break; + case 3: + console.log(datetime + ' ' + colorText('[ERROR]', textColor.Red) + ' ' + message); + break; + default: + console.log(datetime + ' ' + colorText('[INFO]', textColor.White) + ' ' + message); + break; + } +}; + +module.exports = { + logLevels, + textColor, + colorText, + log, +}; \ No newline at end of file diff --git a/pm2-metrics.js b/utils/pm2-metrics.js similarity index 82% rename from pm2-metrics.js rename to utils/pm2-metrics.js index 83168ce..93c723a 100644 --- a/pm2-metrics.js +++ b/utils/pm2-metrics.js @@ -1,3 +1,8 @@ +/* +* Konpeki Shiho - Utility Definition File +* pm2-metrics.js - A place to keep all pm2 metrics and error definitions +*/ + const io = require('@pm2/io'); const interactionErrors = io.counter({