galactic-bot/Logger.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-04-12 14:34:35 +02:00
const { createLogger, format, transports, config } = require('winston');
const { combine, label, printf } = format;
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-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-12 14:34:35 +02:00
this.logger = createLogger({
transports: [
2020-04-12 14:34:35 +02:00
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' })
]
});
2020-04-09 16:30:52 +02:00
2020-04-12 14:34:35 +02:00
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}`;
2020-04-09 16:30:52 +02:00
}
2020-04-08 16:27:34 +02:00
get date() {
2020-04-12 14:34:35 +02:00
return moment().format("MM-DD-YYYY hh:mm:ss");
}
2020-04-08 16:27:34 +02:00
}
module.exports = Logger;