From 4da152f7cc3cd44953a356c944c48474a0b732c7 Mon Sep 17 00:00:00 2001 From: noolaan Date: Mon, 13 Apr 2020 21:25:17 -0600 Subject: [PATCH] logging changes --- logs/error.log | 1 + logs/what.log | 42 +------ middleware/logger/Logger.js | 60 ++++----- .../logger/transports/ExtendedConsole.js | 56 --------- middleware/logger/transports/FileExtension.js | 118 ++++++++++++++++++ middleware/logger/transports/index.js | 2 +- structure/client/DiscordClient.js | 8 -- structure/client/Logger.js | 3 +- 8 files changed, 159 insertions(+), 131 deletions(-) create mode 100644 logs/error.log delete mode 100644 middleware/logger/transports/ExtendedConsole.js create mode 100644 middleware/logger/transports/FileExtension.js diff --git a/logs/error.log b/logs/error.log new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/logs/error.log @@ -0,0 +1 @@ + diff --git a/logs/what.log b/logs/what.log index b5ae471..e96ff89 100644 --- a/logs/what.log +++ b/logs/what.log @@ -1,37 +1,5 @@ -info: Why tf isnt this working... -info: Component module:utility was loaded. -info: Component command:ping was loaded. -info: Component observer:commandHandler was loaded. -info: Client connected to asfasf#1551 with undefined guilds. -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: Component module:utility was loaded. -info: Component command:ping was loaded. -info: Component observer:commandHandler was loaded. -info: Client connected to asfasf#1551 with undefined guilds. -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: [04-12-2020 08:38:18][shard-00] Component module:utility was loaded. -info: [04-12-2020 08:38:18][shard-00] Component command:ping was loaded. -info: [04-12-2020 08:38:18][shard-00] Component observer:commandHandler was loaded. -info: [04-12-2020 08:38:18][shard-00] Client connected to asfasf#1551 with undefined guilds. -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: [04-12-2020 08:39:00][shard-00] Component module:utility was loaded. -info: [04-12-2020 08:39:00][shard-00] Component command:ping was loaded. -info: [04-12-2020 08:39:00][shard-00] Component observer:commandHandler was loaded. -info: [04-12-2020 08:39:00][shard-00] Client connected to asfasf#1551 with undefined guilds. -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: [04-12-2020 08:39:24][shard-00] Component module:utility was loaded. -info: [04-12-2020 08:39:24][shard-00] Component command:ping was loaded. -info: [04-12-2020 08:39:24][shard-00] Component observer:commandHandler was loaded. -info: [04-12-2020 08:39:24][shard-00] Client connected to asfasf#1551 with undefined guilds. -info: Why tf isnt this working... -error: THERES A FUCKIN ERROR -info: [04-12-2020 08:41:07][shard-00] Component module:utility was loaded. -info: [04-12-2020 08:41:07][shard-00] Component command:ping was loaded. -info: [04-12-2020 08:41:07][shard-00] Component observer:commandHandler was loaded. -info: [04-12-2020 08:41:07][shard-00] Client connected to asfasf#1551 with undefined guilds. +debug: [04-13-2020 09:24:35][shard00] : Shard created. +info: [04-13-2020 09:24:35][shard00] : Component module:utility was loaded. +info: [04-13-2020 09:24:35][shard00] : Component command:ping was loaded. +info: [04-13-2020 09:24:35][shard00] : Component observer:commandHandler was loaded. +info: [04-13-2020 09:24:35][shard00] : Client connected to asfasf#1551 with 1 guild. diff --git a/middleware/logger/Logger.js b/middleware/logger/Logger.js index 9fa78ee..f8c1f3e 100644 --- a/middleware/logger/Logger.js +++ b/middleware/logger/Logger.js @@ -3,9 +3,7 @@ const { combine, label, printf } = format; const moment = require('moment'); const chalk = require('chalk'); -const { DiscordWebhook, ExtendedConsole } = require('./transports/'); - -const regex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g //removes chalk formatting, will be used for the log FILES. +const { DiscordWebhook, FileExtension } = require('./transports/'); class Logger { @@ -15,26 +13,33 @@ class Logger { this.shardManager = manager.shardManager; this.logger = createLogger({ + levels: config.npm.levels, format: ( - format.simple() + format.cli({ + colors: { + error: 'red', + warn: 'yellow', + info: 'blue', + verbose: 'cyan', + debug: 'magenta', + silly: 'magentaBright' + } + }) ), transports: [ - new ExtendedConsole(), - //new transports.Console(), - new transports.File({ filename: `logs/what.log` }), - new transports.File({ filename: `logs/${this.date.replace(/ /g, '-')}-error.log`, level: 'error' }), + new FileExtension({ filename: `logs/what.log`, level: 'debug'}), //Will NOT log "silly" logs, could change in future. + new FileExtension({ filename: `logs/error.log` , level: 'error' }), + new transports.Console({ level: 'silly' }), //Will log EVERYTHING. new DiscordWebhook({ level: 'error' }) //Broadcast errors to a discord webhook. ] }); + //TODO: Add proper date-oriented filenames and add a daily rotation file (?). + this.shardManager .on('shardCreate', (shard) => this.write(shard, "Shard created.", 'debug')) .on('message', (shard, message) => this._handleMessage(shard, message)); - //console.log("FUCK"); - this.logger.log('info', "Why tf isnt this working..."); - this.logger.log('error', "THERES A FUCKIN ERROR"); - } //Messages coming from the shards process.send functions. @@ -44,21 +49,21 @@ class Logger { || message._reconnecting || message._sFetchProp || message._sEval - || message._sRespawnAll) return undefined; //Properties used for discord.js internal sharding, must filter for. + || message._sRespawnAll + || message._storage + || message._webhook) return undefined; //Properties used for discord.js internal sharding & our own structures. must filter for. await this.write(shard, message.message, message.type); } - //The MAIN function for writing everything to the logger. async write(shard, string = '', type = 'silly') { - if(!config.npm.levels[type]) return undefined; const color = Constants.Colors[type]; - const header = `[${this.date}][shard-${this._shardId(shard)}]`;//`${chalk[color](`[${this.date}][shard-${this._shardId(shard)}]`)}`; - //[04/02/2020 12:52:20][shard-00] + const header = `${chalk[color](`[${this.date}][${shard ? `shard${this._shardId(shard)}` : 'manager'}]`)}`; - this.logger.log(type, `${header} ${string}`); + + this.logger.log(type, `${header} : ${string}`); } @@ -86,13 +91,12 @@ const Constants = { } }; -/* -const levels = { - error: 0, - warn: 1, - info: 2, - http: 3, - verbose: 4, - debug: 5, - silly: 6 -};*/ \ No newline at end of file +// const levels = { +// error: 0, +// warn: 1, +// info: 2, +// http: 3, +// verbose: 4, +// debug: 5, +// silly: 6 +// }; \ No newline at end of file diff --git a/middleware/logger/transports/ExtendedConsole.js b/middleware/logger/transports/ExtendedConsole.js deleted file mode 100644 index af5a2e5..0000000 --- a/middleware/logger/transports/ExtendedConsole.js +++ /dev/null @@ -1,56 +0,0 @@ -const Transport = require('winston-transport'); -const moment = require('moment'); -const chalk = require('chalk'); - -const Constants = { - Colors: { - error: 'red', - warn: 'yellow', - info: 'blue', - verbose: 'cyan', - debug: 'magenta', - silly: 'magentaBright' - } -}; - -class ExtendedConsole extends Transport { - - constructor(opts) { - super(opts); - // - // Consume any custom options here. e.g.: - // - Connection information for databases - // - Authentication information for APIs (e.g. loggly, papertrail, - // logentries, etc.). - // - } - - log(info, callback) { - setImmediate(() => { - this.emit('logged', info); - }); - - //console.log('Extended console:'); - //console.log(info); - - const hdr = info.message.match(/^(\[[0-9\-: ]{19}\](\[shard-[0-9]{2}\])?)/); - if(hdr) info.message = info.message.replace(hdr[1], '').trim(); - - const color = Constants.Colors[info.level]; - const header = `${chalk[color](hdr ? hdr[1] : `[${this.date}]`)}`; - - console.log(`${header} ${info.message}`); - - if (callback) { - callback(); // eslint-disable-line callback-return - } - - } - - get date() { - return moment().format("MM-DD-YYYY hh:mm:ss"); - } - -} - -module.exports = ExtendedConsole; \ No newline at end of file diff --git a/middleware/logger/transports/FileExtension.js b/middleware/logger/transports/FileExtension.js new file mode 100644 index 0000000..ef48f80 --- /dev/null +++ b/middleware/logger/transports/FileExtension.js @@ -0,0 +1,118 @@ +const { transports: { File }} = require('winston'); +const debug = require('diagnostics')('winston:file'); +const { MESSAGE } = require('triple-beam'); +const moment = require('moment'); +const chalk = require('chalk'); + +const regex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g + +class FileExtension extends File { + + constructor(opts) { + super(opts); + } + + log(info, callback = () => {}) { + // Remark: (jcrugzz) What is necessary about this callback(null, true) now + // when thinking about 3.x? Should silent be handled in the base + // TransportStream _write method? + if (this.silent) { + callback(); + return true; + } + + // Output stream buffer is full and has asked us to wait for the drain event + if (this._drain) { + this._stream.once('drain', () => { + this._drain = false; + this.log(info, callback); + }); + return; + } + if (this._rotate) { + this._stream.once('rotate', () => { + this._rotate = false; + this.log(info, callback); + }); + return; + } + + // Grab the raw string and append the expected EOL. + const output = `${info[MESSAGE]}${this.eol}`.replace(regex, '') + const bytes = Buffer.byteLength(output); + + // After we have written to the PassThrough check to see if we need + // to rotate to the next file. + // + // Remark: This gets called too early and does not depict when data + // has been actually flushed to disk. + function logged() { + this._size += bytes; + this._pendingSize -= bytes; + + debug('logged %s %s', this._size, output); + this.emit('logged', info); + + // Do not attempt to rotate files while opening + if (this._opening) { + return; + } + + // Check to see if we need to end the stream and create a new one. + if (!this._needsNewFile()) { + return; + } + + // End the current stream, ensure it flushes and create a new one. + // This could potentially be optimized to not run a stat call but its + // the safest way since we are supporting `maxFiles`. + this._rotate = true; + this._endStream(() => this._rotateFile()); + } + + // Keep track of the pending bytes being written while files are opening + // in order to properly rotate the PassThrough this._stream when the file + // eventually does open. + this._pendingSize += bytes; + if (this._opening + && !this.rotatedWhileOpening + && this._needsNewFile(this._size + this._pendingSize)) { + this.rotatedWhileOpening = true; + } + + const written = this._stream.write(output, logged.bind(this)); + if (!written) { + this._drain = true; + this._stream.once('drain', () => { + this._drain = false; + callback(); + }); + } else { + callback(); // eslint-disable-line callback-return + } + + debug('written', written, this._drain); + + this.finishIfEnding(); + + return written; + } + + get date() { + return moment().format("MM-DD-YYYY hh:mm:ss"); + } + +} + +module.exports = FileExtension; + +const Constants = { + Colors: { + error: 'red', + warn: 'yellow', + info: 'blue', + verbose: 'cyan', + debug: 'magenta', + silly: 'magentaBright' + } +}; \ No newline at end of file diff --git a/middleware/logger/transports/index.js b/middleware/logger/transports/index.js index 1f87e7e..9602b2f 100644 --- a/middleware/logger/transports/index.js +++ b/middleware/logger/transports/index.js @@ -1,5 +1,5 @@ /* eslint-disable linebreak-style */ module.exports = { DiscordWebhook: require('./DiscordWebhook.js'), - ExtendedConsole: require('./ExtendedConsole.js') + FileExtension: require('./FileExtension.js') }; \ No newline at end of file diff --git a/structure/client/DiscordClient.js b/structure/client/DiscordClient.js index 8a99de0..76d8be8 100644 --- a/structure/client/DiscordClient.js +++ b/structure/client/DiscordClient.js @@ -32,8 +32,6 @@ class DiscordClient extends Client { if(this._built) return undefined; - console.log('Building Discord client'); - await super.login(this._options.bot.token); await this.registry.loadComponents('components/commands/', Command); @@ -43,12 +41,6 @@ class DiscordClient extends Client { this._built = true; - this.on('ready', () => { - console.log('Client websocket is ready.'); - }); - - console.log('Client built'); - } diff --git a/structure/client/Logger.js b/structure/client/Logger.js index d1fca62..5885d98 100644 --- a/structure/client/Logger.js +++ b/structure/client/Logger.js @@ -7,7 +7,8 @@ class Logger { this.client = client; this.client.eventHooker.hook('ready', () => { - this.info(`Client connected to ${chalk.bold(this.client.user.tag)} with ${chalk.bold(`${this.client.guilds.size} guild${this.client.guilds.size === 1 ? '' : 's'}`)}.`); + 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 }) => {