const { createLogger, format, transports, config } = require('winston'); const { combine, label, printf } = format; const moment = require('moment'); const chalk = require('chalk'); const { DiscordWebhook, FileExtension } = require('./transports/'); class Logger { constructor(manager) { this.manager = manager; this.shardManager = manager.shardManager; this.logger = createLogger({ levels: config.npm.levels, format: ( format.cli({ colors: { error: 'red', warn: 'yellow', info: 'blue', verbose: 'cyan', debug: 'magenta', silly: 'magentaBright' } }) ), transports: [ 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)); } //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 || 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); } async write(shard, string = '', type = 'silly') { const color = Constants.Colors[type]; const header = `${chalk[color](`[${this.date}][${shard ? `shard${this._shardId(shard)}` : 'manager'}]`)}`; this.logger.log(type, `${header} : ${string}`); } _shardId(shard) { const id = shard.id; return `${id}`.length === 1 ? `0${id}` : `${id}`; } get date() { return moment().format("MM-DD-YYYY hh:mm:ss"); } } 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 // };