modmail/logger/Logger.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-06-18 15:41:57 +02:00
const { createLogger, format, transports: { Console }, config } = require('winston');
const moment = require('moment');
const chalk = require('chalk');
const { DiscordWebhook, FileExtension } = require('./transports/index.js');
const Constants = {
Colors: {
error: 'red',
warn: 'yellow',
info: 'blue',
verbose: 'cyan',
debug: 'magenta',
silly: 'magentaBright'
}
};
class Logger {
2022-01-12 17:29:14 +01:00
constructor (client, options) {
2021-06-18 15:41:57 +02:00
this.client = client;
this.options = options;
const transports = [
2022-01-12 17:29:14 +01:00
new FileExtension({ filename: `logs/${this.date.split(' ')[0]}.log`, level: 'debug' }), // Will NOT log "silly" logs, could change in future.
2021-06-18 15:41:57 +02:00
new FileExtension({ filename: `logs/errors/${this.date.split(' ')[0]}-error.log`, level: 'error' }),
2022-01-12 17:29:14 +01:00
new Console({ level: 'silly' }) // Will log EVERYTHING.
2021-06-18 15:41:57 +02:00
];
2022-01-12 17:29:14 +01:00
if (!options.webhook.disabled) transports.push(new DiscordWebhook({ level: 'error', ...options.webhook })); // Broadcast errors to a discord webhook.
2021-06-18 15:41:57 +02:00
this.logger = createLogger({
levels: config.npm.levels,
format:
format.cli({
colors: Constants.Colors
}),
transports
});
2022-01-12 17:29:14 +01:00
// TODO: Add proper date-oriented filenames and add a daily rotation file (?).
2021-06-18 15:41:57 +02:00
}
2022-01-12 17:29:14 +01:00
write (type = 'silly', string = '') {
2021-06-18 15:41:57 +02:00
const color = Constants.Colors[type];
const header = `${chalk[color](`[${this.date}][modmail]`)}`;
this.logger.log(type, `${header} : ${string}`);
}
2022-01-12 17:29:14 +01:00
get date () {
2021-06-18 15:41:57 +02:00
return moment().format("YYYY-MM-DD hh:mm:ss");
}
2022-01-12 17:29:14 +01:00
info (message) {
2021-06-18 15:41:57 +02:00
this.write('info', message);
}
2022-01-12 17:29:14 +01:00
warn (message) {
2021-06-18 15:41:57 +02:00
this.write('warn', message);
}
2022-01-12 17:29:14 +01:00
error (message) {
2021-06-18 15:41:57 +02:00
this.write('error', message);
}
2022-01-12 17:29:14 +01:00
debug (message) {
2021-06-18 15:41:57 +02:00
this.write('debug', message);
}
}
module.exports = Logger;