require('colors'); let fs = require('./fs.js'); class Logger { constructor(options) { this.events = options.events || {emit: function(){}}; this.logLevels = ['error', 'warn', 'info', 'debug', 'trace']; this.logLevel = options.logLevel || 'info'; this.logFunction = options.logFunction || console.log; this.logFile = options.logFile; } } Logger.prototype.writeToFile = function () { if (!this.logFile) { return; } fs.appendFileSync(this.logFile, "\n" + Array.from(arguments).join(' ')); }; Logger.prototype.error = function () { if (!arguments.length || !(this.shouldLog('error'))) { return; } this.events.emit("log", "error", ...arguments); this.logFunction(...Array.from(arguments).map(t => t.red)); this.writeToFile("[error]: ", ...arguments); }; Logger.prototype.warn = function () { if (!arguments.length || !(this.shouldLog('warn'))) { return; } this.events.emit("log", "warning", ...arguments); this.logFunction(...Array.from(arguments).map(t => t.yellow)); this.writeToFile("[warning]: ", ...arguments); }; Logger.prototype.info = function () { if (!arguments.length || !(this.shouldLog('info'))) { return; } this.events.emit("log", "info", ...arguments); this.logFunction(...Array.from(arguments).map(t => t.green)); this.writeToFile("[info]: ", ...arguments); }; Logger.prototype.debug = function () { if (!arguments.length || !(this.shouldLog('debug'))) { return; } this.events.emit("log", "debug", ...arguments); this.logFunction(...arguments); this.writeToFile("[debug]: ", ...arguments); }; Logger.prototype.trace = function () { if (!arguments.length || !(this.shouldLog('trace'))) { return; } this.events.emit("log", "trace", ...arguments); this.logFunction(...arguments); this.writeToFile("[trace]: ", ...arguments); }; 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); }; Logger.prototype.shouldLog = function (level) { return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel)); }; module.exports = Logger;