From 7735a9fc98ef67eaed122ceb4dc5d75dbf5b4f74 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 12 Apr 2020 15:34:35 +0300 Subject: [PATCH] main logger --- Logger.js | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/Logger.js b/Logger.js index 5e56eaf..990de5b 100644 --- a/Logger.js +++ b/Logger.js @@ -1,23 +1,58 @@ -const winston = require('winston'); +const { createLogger, format, transports, config } = require('winston'); +const { combine, label, printf } = format; const moment = require('moment'); +const chalk = require('chalk'); class Logger { constructor(manager) { this.manager = manager; - this.logger = winston.createLogger({ + this.logger = createLogger({ transports: [ - new winston.transports.Console(), - new winston.transports.File({ filename: `logs/${this.date}.log` }), - new winston.transports.File({ filename: `logs/${this.date}-error.log`, level: 'error' }) + new transports.Console(), + new transports.File({ filename: `logs/${this.date.replace(/ /g, '-')}.log` }), + new transports.File({ filename: `logs/${this.date.replace(/ /g, '-')}-error.log`, level: 'error' }) ] }); + this.client + .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) return undefined; //Properties used for discord.js internal sharding, must filter for. + + + + } + + //The MAIN function for writing everything to the logger. + async write(shard, string = '', type = 'silly') { + if(!config.npm.levels[type]) return undefined; + + const header = `[${this.date}][shard-${this._shardId(shard)}]`; + //[04/02/2020 12:52:20][shard-00] + + this.logger.log(type, 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"); + return moment().format("MM-DD-YYYY hh:mm:ss"); } }