2022-01-27 00:11:14 +01:00
|
|
|
/* eslint-disable max-len */
|
2021-08-16 23:01:08 +02:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const moment = require('moment');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const Util = require('../Util.js');
|
|
|
|
|
|
|
|
const Constants = {
|
|
|
|
Types: [
|
|
|
|
'error',
|
|
|
|
'warn',
|
|
|
|
'info',
|
|
|
|
'debug',
|
|
|
|
'status'
|
|
|
|
],
|
|
|
|
Colors: {
|
|
|
|
error: 'red',
|
|
|
|
warn: 'yellow',
|
|
|
|
info: 'blue',
|
|
|
|
debug: 'magenta',
|
|
|
|
status: 'cyanBright'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const stripRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/gu; //eslint-disable-line no-control-regex
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
|
|
|
constructor(manager, options) {
|
|
|
|
|
|
|
|
this.manager = manager;
|
|
|
|
this.shardingManager = manager.shardingManager;
|
|
|
|
|
|
|
|
this._options = options;
|
|
|
|
|
|
|
|
this._writeStream = this._loadFile();
|
|
|
|
this._errorWriteStream = this._loadFile('error');
|
|
|
|
|
|
|
|
this.shardingManager
|
|
|
|
.on('shardCreate', (shard) => {
|
|
|
|
this.write('debug', "Shard created.", shard);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:56:59 +02:00
|
|
|
write(type = 'info', string = '', shard = null, api = false) {
|
2021-08-16 23:01:08 +02:00
|
|
|
|
|
|
|
type = type.toLowerCase();
|
|
|
|
|
|
|
|
let color = Constants.Colors[type];
|
|
|
|
if(!color) color = Constants.Colors.info;
|
|
|
|
|
2021-08-25 22:15:54 +02:00
|
|
|
const header = `${chalk[color](`[${Util.date}][${shard ? `${api ? 'api-s' : 'shard'}${this._shardId(shard)}` : `${api ? 'api-man' : 'manager'}`}]`)}`;
|
2021-08-16 23:01:08 +02:00
|
|
|
|
|
|
|
const maximumCharacters = Math.max(...Constants.Types.map((t) => t.length));
|
|
|
|
const spacers = maximumCharacters - type.length;
|
|
|
|
const text = `${chalk[color](type)}${" ".repeat(spacers)} ${header} : ${string}`;
|
|
|
|
|
|
|
|
console.log(text); //eslint-disable-line no-console
|
|
|
|
|
|
|
|
if(this[type === 'error' ? '_errorWriteStream' : '_writeStream']) {
|
|
|
|
const strippedText = text.replace(stripRegex, '');
|
|
|
|
this[type === 'error' ? '_errorWriteStream' : '_writeStream']
|
|
|
|
.write(`\n${strippedText}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadFile(type = null) {
|
|
|
|
const filename = `${moment().format('YYYY-MM-DD')}${type ? `-${type}` : ''}`;
|
|
|
|
const directory = path.join(process.cwd(), this._options.directory);
|
|
|
|
|
|
|
|
//Ensuring that the log folders exist.
|
|
|
|
if(!fs.existsSync(directory)) fs.mkdirSync(directory);
|
|
|
|
|
|
|
|
//Creating the log files.
|
|
|
|
const filepath = path.join(directory, `${filename}.log`);
|
|
|
|
|
|
|
|
let filedirectory = null;
|
|
|
|
if(!fs.existsSync(filepath)) {
|
|
|
|
fs.writeFileSync(filepath, '');
|
|
|
|
filedirectory = filepath;
|
|
|
|
this.write('debug', `Creating new${type ? ` ${type} ` : ' '}log file at: ${chalk.bold(`"${filepath}"`)}`);
|
|
|
|
} else {
|
|
|
|
this.write('debug', `Found existing${type ? ` ${type} ` : ' '}log file, using: ${chalk.bold(`"${filepath}"`)}`);
|
|
|
|
filedirectory = filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filedirectory) {
|
|
|
|
return fs.createWriteStream(filedirectory, { flags: 'a' }); //Appends to file.
|
|
|
|
}
|
|
|
|
|
|
|
|
this.write('error', "Unable to open write stream for log file, proceeding without one.");
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Messages coming from the shards process.send functions.
|
|
|
|
_handleMessage(shard, message) {
|
2021-08-24 14:56:59 +02:00
|
|
|
this.write(message.type, message.message, shard, message._api);
|
2021-08-16 23:01:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_shardId(shard) {
|
|
|
|
const { id } = shard;
|
|
|
|
return `${id}`.length === 1 ? `0${id}` : `${id}`;
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:15:15 +02:00
|
|
|
error(message) {
|
|
|
|
this.write('error', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(message) {
|
|
|
|
this.write('warn', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(message) {
|
|
|
|
this.write('debug', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
info(message) {
|
|
|
|
this.write('info', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
status(message) {
|
|
|
|
this.write('status', message);
|
|
|
|
}
|
|
|
|
|
2021-08-16 23:01:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Logger;
|