2020-04-12 14:34:35 +02:00
|
|
|
const { createLogger, format, transports, config } = require('winston');
|
|
|
|
const { combine, label, printf } = format;
|
2020-04-09 23:08:28 +02:00
|
|
|
const moment = require('moment');
|
2020-04-12 14:34:35 +02:00
|
|
|
const chalk = require('chalk');
|
2020-04-08 16:27:34 +02:00
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
const { DiscordWebhook } = 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.
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
class Logger {
|
2020-04-08 16:27:34 +02:00
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
constructor(manager) {
|
|
|
|
|
|
|
|
this.manager = manager;
|
2020-04-13 22:38:10 +02:00
|
|
|
this.shardManager = manager.shardManager;
|
|
|
|
|
2020-04-12 14:34:35 +02:00
|
|
|
this.logger = createLogger({
|
2020-04-13 22:38:10 +02:00
|
|
|
format: (
|
|
|
|
format.simple()
|
|
|
|
),
|
2020-04-09 23:08:28 +02:00
|
|
|
transports: [
|
2020-04-12 14:34:35 +02:00
|
|
|
new transports.Console(),
|
2020-04-13 22:38:10 +02:00
|
|
|
new transports.File({ filename: `logs/what.log` }),
|
|
|
|
new transports.File({ filename: `logs/${this.date.replace(/ /g, '-')}-error.log`, level: 'error' }),
|
|
|
|
new DiscordWebhook({ level: 'error' }) //Broadcast errors to a discord webhook.
|
2020-04-09 23:08:28 +02:00
|
|
|
]
|
|
|
|
});
|
2020-04-09 16:30:52 +02:00
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
this.shardManager
|
|
|
|
.on('shardCreate', (shard) => this.write(shard, "Shard created.", 'debug'))
|
2020-04-12 14:34:35 +02:00
|
|
|
.on('message', (shard, message) => this._handleMessage(shard, message));
|
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
console.log("FUCK");
|
|
|
|
this.logger.log('info', "Why tf isnt this working...");
|
|
|
|
this.logger.log('error', "THERES A FUCKIN ERROR");
|
|
|
|
|
2020-04-12 14:34:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Messages coming from the shards process.send functions.
|
|
|
|
async _handleMessage(shard, message) {
|
|
|
|
if(message._ready
|
|
|
|
|| message._disconnect
|
|
|
|
|| message._reconnecting
|
|
|
|
|| message._sFetchProp
|
|
|
|
|| message._sEval
|
|
|
|
|| message._sRespawnAll) return undefined; //Properties used for discord.js internal sharding, must filter for.
|
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
await this.write(shard, message.message, message.type);
|
2020-04-12 14:34:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//The MAIN function for writing everything to the logger.
|
|
|
|
async write(shard, string = '', type = 'silly') {
|
|
|
|
if(!config.npm.levels[type]) return undefined;
|
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
const color = Constants.Colors[type];
|
|
|
|
const header = `${chalk[color](`[${this.date}][shard-${this._shardId(shard)}]`)}`;
|
2020-04-12 14:34:35 +02:00
|
|
|
//[04/02/2020 12:52:20][shard-00]
|
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
this.logger.log(type, `${header} ${string}`)
|
2020-04-12 14:34:35 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_shardId(shard) {
|
|
|
|
const id = shard.id;
|
|
|
|
return `${id}`.length === 1 ? `0${id}` : `${id}`;
|
2020-04-09 16:30:52 +02:00
|
|
|
}
|
2020-04-08 16:27:34 +02:00
|
|
|
|
2020-04-09 23:08:28 +02:00
|
|
|
get date() {
|
2020-04-12 14:34:35 +02:00
|
|
|
return moment().format("MM-DD-YYYY hh:mm:ss");
|
2020-04-09 23:08:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:38:10 +02:00
|
|
|
module.exports = Logger;
|
|
|
|
|
|
|
|
const Constants = {
|
|
|
|
Colors: {
|
|
|
|
error: 'red',
|
|
|
|
warn: 'yellow',
|
|
|
|
info: 'blue',
|
|
|
|
verbose: 'cyan',
|
|
|
|
debug: 'magenta',
|
|
|
|
silly: 'magentaBright'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
const levels = {
|
|
|
|
error: 0,
|
|
|
|
warn: 1,
|
|
|
|
info: 2,
|
|
|
|
http: 3,
|
|
|
|
verbose: 4,
|
|
|
|
debug: 5,
|
|
|
|
silly: 6
|
|
|
|
};*/
|