From 5922b5153960558953bfaf0cedb3377ca0d3cf3a Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 15 Mar 2022 02:00:45 +0200 Subject: [PATCH] slight logger rework --- src/structure/client/Dispatcher.js | 4 ++- src/structure/client/EventHooker.js | 5 +++ src/structure/client/LocaleLoader.js | 7 ++-- src/structure/client/Logger.js | 53 +++------------------------- src/structure/client/Registry.js | 10 +++--- 5 files changed, 24 insertions(+), 55 deletions(-) diff --git a/src/structure/client/Dispatcher.js b/src/structure/client/Dispatcher.js index 8fef550..2bb21f1 100644 --- a/src/structure/client/Dispatcher.js +++ b/src/structure/client/Dispatcher.js @@ -1,4 +1,5 @@ const { Collection } = require('@discordjs/collection'); +const Logger = require('./Logger'); class Dispatcher { @@ -6,6 +7,7 @@ class Dispatcher { this.client = client; this.dispatched = new Collection(); + this.logger = new Logger(this); this.client.eventHooker.hook('componentUpdate', ({ component, type }) => { if(component.type === 'observer') { @@ -29,7 +31,7 @@ class Dispatcher { .filter((c) => c._type === 'observer' && !c.disabled) .sort((a, b) => a.priority - b.priority); - this.client.logger.debug(`Starting dispatch on ${observers.size} observers.`); + this.logger.debug(`Starting dispatch on ${observers.size} observers.`); for(const observer of observers.values()) { for(const [hook, func] of observer.hooks) { this.client.eventHooker.hook(hook, func); diff --git a/src/structure/client/EventHooker.js b/src/structure/client/EventHooker.js index 7c9db07..b446d58 100644 --- a/src/structure/client/EventHooker.js +++ b/src/structure/client/EventHooker.js @@ -1,4 +1,5 @@ const { EventEmitter } = require('events'); +const Logger = require('./Logger'); const { InteractionWrapper, GuildWrapper } = require('./wrappers'); class EventHooker { @@ -8,10 +9,12 @@ class EventHooker { this.target = target; this.events = new Map(); + this.logger = new Logger(this); } hook(eventName, func) { + this.logger.debug(`Hooking ${eventName} to ${func.name}`); if(this.events.has(eventName)) { const funcs = this.events.get(eventName); this.events.set(eventName, [ ...funcs, func ]); @@ -33,7 +36,9 @@ class EventHooker { } async _handleEvent(eventName) { + this.logger.debug(`Setting up handler for ${eventName}`); this.target.on(eventName, async (...args) => { + this.logger.debug(`Handler ${eventName}`); // Should probably move this elsewhere, but testing this out for (const arg of args) if (arg?.guild) arg.guildWrapper = new GuildWrapper(this.target, arg.guild); diff --git a/src/structure/client/LocaleLoader.js b/src/structure/client/LocaleLoader.js index dca4c6f..5950551 100644 --- a/src/structure/client/LocaleLoader.js +++ b/src/structure/client/LocaleLoader.js @@ -4,6 +4,7 @@ const chalk = require('chalk'); const Util = require('../../Util.js'); const { Emojis } = require('../../constants/index.js'); +const Logger = require('./Logger.js'); class LocaleLoader { @@ -13,6 +14,8 @@ class LocaleLoader { this.languages = {}; + this.logger = new Logger(this); + } async loadLanguages() { @@ -40,7 +43,7 @@ class LocaleLoader { try { string = eval(string); //eslint-disable-line no-eval } catch(error) { - this.client.logger.error(`Locale [${language}.${index}] failed to execute code.\n${error.stack || error}`); + this.logger.error(`Locale [${language}.${index}] failed to execute code.\n${error.stack || error}`); } } @@ -63,7 +66,7 @@ class LocaleLoader { } this.languages[language] = combined; - this.client.logger.info(`Language ${chalk.bold(language)} was ${chalk.bold("loaded")}.`); + this.logger.info(`Language ${chalk.bold(language)} was ${chalk.bold("loaded")}.`); } diff --git a/src/structure/client/Logger.js b/src/structure/client/Logger.js index aae0473..324d6ad 100644 --- a/src/structure/client/Logger.js +++ b/src/structure/client/Logger.js @@ -2,64 +2,21 @@ const chalk = require('chalk'); const { inspect } = require('util'); -const Constants = { - ComponentTypes: { - LOAD: 'loaded', - UNLOAD: 'unloaded', - RELOAD: 'reloaded', - ENABLE: 'enabled', - DISABLE: 'disabled' - } -}; - class Logger { constructor(client) { this.client = client; - - this.client.eventHooker.hook('ready', () => { - const guilds = this.client.guilds.cache.size; - this.info(`Client connected to ${chalk.bold(this.client.user.tag)} with ${chalk.bold(`${guilds} guild${guilds === 1 ? '' : 's'}`)}.`); - }); - - this.client.eventHooker.hook('componentUpdate', ({ component, type }) => { - this.info(`Component ${chalk.bold(component.resolveable)} was ${chalk.bold(Constants.ComponentTypes[type])}.`); //eslint-disable-line no-use-before-define - }); - - this.client.eventHooker.hook('guildCreate', (guild) => { - this.debug(`${chalk.bold('[GUILD]')} Joined guild ${chalk.bold(guild.name)} (${guild.id}).`); - }); - - this.client.eventHooker.hook('guildDelete', (guild) => { - this.debug(`${chalk.bold('[GUILD]')} Left guild ${chalk.bold(guild.name)} (${guild.id}).`); - }); - - this.client.eventHooker.hook('shardDisconect', () => { - this.status('Shard disconnected.'); - }); - - this.client.eventHooker.hook('shardError', (error) => { - this.status(`Shard errored:\n${error.stack ?? error}`); - }); - - this.client.eventHooker.hook('shardReady', (id, unavailableGuilds) => { - this.status(`Shard is ready${unavailableGuilds ? ` with ${chalk.bold(`${[...unavailableGuilds].length} unavailable guilds`)}` : ''}.`); - }); - - this.client.eventHooker.hook('shardReconnecting', () => { - this.status('Shard is reconnecting.'); - }); - - this.client.eventHooker.hook('shardResume', () => { - this.status('Shard resumed.'); - }); + this.name = client.name || client.constructor.name; } async transport(message = 'N/A', opts = {}) { if (typeof message !== 'string') message = inspect(message); - return this.client.intercom.send('logger', { message, ...opts }); + message = `${chalk.bold(`[${this.name.substring(0, 6)}]`)} ${message}`; + + process.send({ _logger: true, message, ...opts }); + //return this.client.intercom.send('logger', { message, ...opts }); } /* Quick & Dirty Functions */ diff --git a/src/structure/client/Registry.js b/src/structure/client/Registry.js index c8b7182..edcca1a 100644 --- a/src/structure/client/Registry.js +++ b/src/structure/client/Registry.js @@ -3,6 +3,7 @@ const path = require('path'); const { Component, Module } = require('../interfaces/'); const Util = require('../../Util.js'); +const Logger = require('./Logger'); class Registry { @@ -11,6 +12,7 @@ class Registry { this.client = client; this.components = new Collection(); + this.logger = new Logger(this); } @@ -32,14 +34,14 @@ class Registry { for(const path of files) { const func = require(path); if(typeof func !== 'function') { - this.client.logger.warn("Attempted to index an invalid function as a component."); + this.logger.warn("Attempted to index an invalid function as a component."); delete require.cache[path]; continue; } const component = new func(this.client); //Instantiates the component class. if(classToHandle && !(component instanceof classToHandle)) { - this.client.logger.warn("Attempted to load an invalid class."); + this.logger.warn("Attempted to load an invalid class."); delete require.cache[path]; continue; } @@ -65,12 +67,12 @@ class Registry { if(!(component instanceof Component)) { delete require.cache[directory]; - this.client.logger.warn("Attempted to load an invalid component."); + this.logger.warn("Attempted to load an invalid component."); return null; } if(this.components.has(component.resolveable)) { - this.client.logger.warn("Attempted to reload an existing component."); + this.logger.warn("Attempted to reload an existing component."); return null; }