Make logs more readable

This commit is contained in:
Michael 2022-12-16 04:58:49 -05:00
parent 0b8abbaf42
commit dd882eb048
3 changed files with 75 additions and 5 deletions

View file

@ -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

62
utils/logging.js Normal file
View file

@ -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,
};

View file

@ -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({