logging changes

This commit is contained in:
noolaan 2020-04-13 21:25:17 -06:00
parent fffdaf5711
commit 4da152f7cc
8 changed files with 159 additions and 131 deletions

1
logs/error.log Normal file
View File

@ -0,0 +1 @@

View File

@ -1,37 +1,5 @@
info: Why tf isnt this working... debug: [04-13-2020 09:24:35][shard00] : Shard created.
info: Component module:utility was loaded. info: [04-13-2020 09:24:35][shard00] : Component module:utility was loaded.
info: Component command:ping was loaded. info: [04-13-2020 09:24:35][shard00] : Component command:ping was loaded.
info: Component observer:commandHandler was loaded. info: [04-13-2020 09:24:35][shard00] : Component observer:commandHandler was loaded.
info: Client connected to asfasf#1551 with undefined guilds. info: [04-13-2020 09:24:35][shard00] : Client connected to asfasf#1551 with 1 guild.
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.

View File

@ -3,9 +3,7 @@ const { combine, label, printf } = format;
const moment = require('moment'); const moment = require('moment');
const chalk = require('chalk'); const chalk = require('chalk');
const { DiscordWebhook, ExtendedConsole } = require('./transports/'); const { DiscordWebhook, FileExtension } = 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.
class Logger { class Logger {
@ -15,26 +13,33 @@ class Logger {
this.shardManager = manager.shardManager; this.shardManager = manager.shardManager;
this.logger = createLogger({ this.logger = createLogger({
levels: config.npm.levels,
format: ( format: (
format.simple() format.cli({
colors: {
error: 'red',
warn: 'yellow',
info: 'blue',
verbose: 'cyan',
debug: 'magenta',
silly: 'magentaBright'
}
})
), ),
transports: [ transports: [
new ExtendedConsole(), new FileExtension({ filename: `logs/what.log`, level: 'debug'}), //Will NOT log "silly" logs, could change in future.
//new transports.Console(), new FileExtension({ filename: `logs/error.log` , level: 'error' }),
new transports.File({ filename: `logs/what.log` }), new transports.Console({ level: 'silly' }), //Will log EVERYTHING.
new transports.File({ filename: `logs/${this.date.replace(/ /g, '-')}-error.log`, level: 'error' }),
new DiscordWebhook({ level: 'error' }) //Broadcast errors to a discord webhook. new DiscordWebhook({ level: 'error' }) //Broadcast errors to a discord webhook.
] ]
}); });
//TODO: Add proper date-oriented filenames and add a daily rotation file (?).
this.shardManager this.shardManager
.on('shardCreate', (shard) => this.write(shard, "Shard created.", 'debug')) .on('shardCreate', (shard) => this.write(shard, "Shard created.", 'debug'))
.on('message', (shard, message) => this._handleMessage(shard, message)); .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. //Messages coming from the shards process.send functions.
@ -44,21 +49,21 @@ class Logger {
|| message._reconnecting || message._reconnecting
|| message._sFetchProp || message._sFetchProp
|| message._sEval || 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); await this.write(shard, message.message, message.type);
} }
//The MAIN function for writing everything to the logger.
async write(shard, string = '', type = 'silly') { async write(shard, string = '', type = 'silly') {
if(!config.npm.levels[type]) return undefined;
const color = Constants.Colors[type]; const color = Constants.Colors[type];
const header = `[${this.date}][shard-${this._shardId(shard)}]`;//`${chalk[color](`[${this.date}][shard-${this._shardId(shard)}]`)}`; const header = `${chalk[color](`[${this.date}][${shard ? `shard${this._shardId(shard)}` : 'manager'}]`)}`;
//[04/02/2020 12:52:20][shard-00]
this.logger.log(type, `${header} ${string}`);
this.logger.log(type, `${header} : ${string}`);
} }
@ -86,13 +91,12 @@ const Constants = {
} }
}; };
/* // const levels = {
const levels = { // error: 0,
error: 0, // warn: 1,
warn: 1, // info: 2,
info: 2, // http: 3,
http: 3, // verbose: 4,
verbose: 4, // debug: 5,
debug: 5, // silly: 6
silly: 6 // };
};*/

View File

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

View File

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

View File

@ -1,5 +1,5 @@
/* eslint-disable linebreak-style */ /* eslint-disable linebreak-style */
module.exports = { module.exports = {
DiscordWebhook: require('./DiscordWebhook.js'), DiscordWebhook: require('./DiscordWebhook.js'),
ExtendedConsole: require('./ExtendedConsole.js') FileExtension: require('./FileExtension.js')
}; };

View File

@ -32,8 +32,6 @@ class DiscordClient extends Client {
if(this._built) return undefined; if(this._built) return undefined;
console.log('Building Discord client');
await super.login(this._options.bot.token); await super.login(this._options.bot.token);
await this.registry.loadComponents('components/commands/', Command); await this.registry.loadComponents('components/commands/', Command);
@ -43,12 +41,6 @@ class DiscordClient extends Client {
this._built = true; this._built = true;
this.on('ready', () => {
console.log('Client websocket is ready.');
});
console.log('Client built');
} }

View File

@ -7,7 +7,8 @@ class Logger {
this.client = client; this.client = client;
this.client.eventHooker.hook('ready', () => { 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 }) => { this.client.eventHooker.hook('componentUpdate', ({ component, type }) => {