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 21:18:20 +00:00
|
|
|
this.events = options.events || {emit: function(){}};
|
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-04-19 04:25:43 +00:00
|
|
|
this.logFile = options.logFile;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.writeToFile = function () {
|
2018-04-19 04:25:43 +00:00
|
|
|
if (!this.logFile) {
|
2018-03-10 18:45:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
fs.appendFileSync(this.logFile, "\n" + Array.from(arguments).join(' '));
|
2018-03-10 18:45:56 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.error = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('error'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "error", ...arguments);
|
2018-07-08 22:21:27 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => { return t ? t.red : t; }));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[error]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.warn = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('warn'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "warning", ...arguments);
|
2018-07-08 22:21:27 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => { return t ? t.yellow : t; }));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[warning]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.info = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('info'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "info", ...arguments);
|
2018-07-08 22:21:27 +00:00
|
|
|
this.logFunction(...Array.from(arguments).map(t => { return t ? t.green : t; }));
|
2018-06-19 18:06:56 +00:00
|
|
|
this.writeToFile("[info]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.debug = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('debug'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "debug", ...arguments);
|
|
|
|
this.logFunction(...arguments);
|
|
|
|
this.writeToFile("[debug]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
|
|
|
|
2018-06-19 18:06:56 +00:00
|
|
|
Logger.prototype.trace = function () {
|
|
|
|
if (!arguments.length || !(this.shouldLog('trace'))) {
|
2017-03-30 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-19 18:06:56 +00:00
|
|
|
this.events.emit("log", "trace", ...arguments);
|
|
|
|
this.logFunction(...arguments);
|
|
|
|
this.writeToFile("[trace]: ", ...arguments);
|
2016-09-17 03:56:25 +00:00
|
|
|
};
|
2016-08-22 03:40:05 +00:00
|
|
|
|
2018-04-09 19:24:01 +00:00
|
|
|
Logger.prototype.dir = function (txt) {
|
|
|
|
if (!txt || !(this.shouldLog('info'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.events.emit("log", "dir", txt);
|
|
|
|
this.logFunction(txt);
|
|
|
|
this.writeToFile("[dir]: ");
|
|
|
|
this.writeToFile(txt);
|
|
|
|
};
|
|
|
|
|
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;
|