embark/lib/core/logger.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-12-05 18:14:46 -05:00
require('colors');
2016-08-21 23:40:05 -04:00
2017-03-30 20:12:39 +09:00
class Logger {
constructor(options) {
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
this.logLevel = options.logLevel || 'info';
this.logFunction = options.logFunction || console.log;
this.contractsState = options.contractsState || function () {
};
this.setStatus = options.setStatus || console.log;
}
}
Logger.prototype.error = function (txt) {
if (!txt || !(this.shouldLog('error'))) {
2017-03-30 20:12:39 +09:00
return;
}
2016-09-16 23:56:25 -04:00
this.logFunction(txt.red);
};
2016-08-21 23:40:05 -04:00
2017-03-30 20:12:39 +09:00
Logger.prototype.warn = function (txt) {
if (!txt || !(this.shouldLog('warn'))) {
2017-03-30 20:12:39 +09:00
return;
}
2016-09-16 23:56:25 -04:00
this.logFunction(txt.yellow);
};
2016-08-21 23:40:05 -04:00
2017-03-30 20:12:39 +09:00
Logger.prototype.info = function (txt) {
if (!txt || !(this.shouldLog('info'))) {
2017-03-30 20:12:39 +09:00
return;
}
2016-09-16 23:56:25 -04:00
this.logFunction(txt.green);
};
2016-08-21 23:40:05 -04:00
2017-03-30 20:12:39 +09:00
Logger.prototype.debug = function (txt) {
if (!txt || !(this.shouldLog('debug'))) {
2017-03-30 20:12:39 +09:00
return;
}
2016-09-16 23:56:25 -04:00
this.logFunction(txt);
};
2017-03-30 20:12:39 +09:00
Logger.prototype.trace = function (txt) {
if (!txt || !(this.shouldLog('trace'))) {
2017-03-30 20:12:39 +09:00
return;
}
2016-09-16 23:56:25 -04:00
this.logFunction(txt);
};
2016-08-21 23:40:05 -04:00
2017-03-30 20:12:39 +09:00
Logger.prototype.shouldLog = function (level) {
2016-09-16 23:56:25 -04:00
return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel));
2016-08-21 23:40:05 -04:00
};
module.exports = Logger;