2017-12-05 23:14:46 +00:00
|
|
|
require('colors');
|
2018-03-10 18:45:56 +00:00
|
|
|
let fs = require('./fs.js');
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Logger {
|
|
|
|
constructor(options) {
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events = options.events;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
|
|
|
|
this.logLevel = options.logLevel || 'info';
|
|
|
|
this.logFunction = options.logFunction || console.log;
|
2018-03-10 18:45:56 +00:00
|
|
|
this.logfile = options.logfile;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 18:45:56 +00:00
|
|
|
Logger.prototype.writeToFile = function (txt) {
|
|
|
|
if (!this.logfile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.appendFileSync(this.logfile, "\n" + txt);
|
|
|
|
};
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.error = function (txt) {
|
2017-04-09 04:55:24 +00:00
|
|
|
if (!txt || !(this.shouldLog('error'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events.emit("log", "error", txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logFunction(txt.red);
|
2018-03-10 18:45:56 +00:00
|
|
|
this.writeToFile("[error]: " + txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.warn = function (txt) {
|
2017-04-09 04:55:24 +00:00
|
|
|
if (!txt || !(this.shouldLog('warn'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events.emit("log", "warning", txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logFunction(txt.yellow);
|
2018-03-10 18:45:56 +00:00
|
|
|
this.writeToFile("[warning]: " + txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.info = function (txt) {
|
2017-04-09 04:55:24 +00:00
|
|
|
if (!txt || !(this.shouldLog('info'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events.emit("log", "info", txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logFunction(txt.green);
|
2018-03-10 18:45:56 +00:00
|
|
|
this.writeToFile("[info]: " + txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.debug = function (txt) {
|
2017-04-09 04:55:24 +00:00
|
|
|
if (!txt || !(this.shouldLog('debug'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events.emit("log", "debug", txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logFunction(txt);
|
2018-03-10 18:45:56 +00:00
|
|
|
this.writeToFile("[debug]: " + txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.trace = function (txt) {
|
2017-04-09 04:55:24 +00:00
|
|
|
if (!txt || !(this.shouldLog('trace'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-15 20:44:05 +00:00
|
|
|
this.events.emit("log", "trace", txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logFunction(txt);
|
2018-03-10 18:45:56 +00:00
|
|
|
this.writeToFile("[trace]: " + txt);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
Logger.prototype.shouldLog = function (level) {
|
2016-09-17 03:56:25 +00:00
|
|
|
return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
|
2016-08-22 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Logger;
|