Merge pull request #561 from embark-framework/bug_fix/intercept-rebased

Fix log interception and enable multi-argument logging #2
This commit is contained in:
Iuri Matias 2018-06-20 16:34:36 -04:00 committed by GitHub
commit b1d9b91890
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 71 deletions

View File

@ -11,57 +11,57 @@ class Logger {
}
}
Logger.prototype.writeToFile = function (txt) {
Logger.prototype.writeToFile = function () {
if (!this.logFile) {
return;
}
fs.appendFileSync(this.logFile, "\n" + txt);
fs.appendFileSync(this.logFile, "\n" + Array.from(arguments).join(' '));
};
Logger.prototype.error = function (txt) {
if (!txt || !(this.shouldLog('error'))) {
Logger.prototype.error = function () {
if (!arguments.length || !(this.shouldLog('error'))) {
return;
}
this.events.emit("log", "error", txt);
this.logFunction(txt.red);
this.writeToFile("[error]: " + txt);
this.events.emit("log", "error", ...arguments);
this.logFunction(...Array.from(arguments).map(t => t.red));
this.writeToFile("[error]: ", ...arguments);
};
Logger.prototype.warn = function (txt) {
if (!txt || !(this.shouldLog('warn'))) {
Logger.prototype.warn = function () {
if (!arguments.length || !(this.shouldLog('warn'))) {
return;
}
this.events.emit("log", "warning", txt);
this.logFunction(txt.yellow);
this.writeToFile("[warning]: " + txt);
this.events.emit("log", "warning", ...arguments);
this.logFunction(...Array.from(arguments).map(t => t.yellow));
this.writeToFile("[warning]: ", ...arguments);
};
Logger.prototype.info = function (txt) {
if (!txt || !(this.shouldLog('info'))) {
Logger.prototype.info = function () {
if (!arguments.length || !(this.shouldLog('info'))) {
return;
}
this.events.emit("log", "info", txt);
this.logFunction(txt.green);
this.writeToFile("[info]: " + txt);
this.events.emit("log", "info", ...arguments);
this.logFunction(...Array.from(arguments).map(t => t.green));
this.writeToFile("[info]: ", ...arguments);
};
Logger.prototype.debug = function (txt) {
if (!txt || !(this.shouldLog('debug'))) {
Logger.prototype.debug = function () {
if (!arguments.length || !(this.shouldLog('debug'))) {
return;
}
this.events.emit("log", "debug", txt);
this.logFunction(txt);
this.writeToFile("[debug]: " + txt);
this.events.emit("log", "debug", ...arguments);
this.logFunction(...arguments);
this.writeToFile("[debug]: ", ...arguments);
};
Logger.prototype.trace = function (txt) {
if (!txt || !(this.shouldLog('trace'))) {
Logger.prototype.trace = function () {
if (!arguments.length || !(this.shouldLog('trace'))) {
return;
}
this.events.emit("log", "trace", txt);
this.logFunction(txt);
this.writeToFile("[trace]: " + txt);
this.events.emit("log", "trace", ...arguments);
this.logFunction(...arguments);
this.writeToFile("[trace]: ", ...arguments);
};
Logger.prototype.dir = function (txt) {

View File

@ -29,7 +29,8 @@ var Plugin = function(options) {
this.afterContractsDeployActions = [];
this.onDeployActions = [];
this.eventActions = {};
this.logger = options.logger;
this._loggerObject = options.logger;
this.logger = this._loggerObject; // Might get changed if we do intercept
this.events = options.events;
this.config = options.config;
this.env = options.env;
@ -45,6 +46,22 @@ var Plugin = function(options) {
}
};
Plugin.prototype._log = function(type) {
this._loggerObject[type](this.name + ':', ...[].slice.call(arguments, 1));
};
Plugin.prototype.setUpLogger = function () {
this.logger = {
log: this._log.bind(this, 'log'),
warn: this._log.bind(this, 'warn'),
error: this._log.bind(this, 'error'),
info: this._log.bind(this, 'info'),
debug: this._log.bind(this, 'debug'),
trace: this._log.bind(this, 'trace'),
dir: this._log.bind(this, 'dir')
};
};
Plugin.prototype.isContextValid = function() {
if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) {
return true;
@ -65,7 +82,7 @@ Plugin.prototype.loadPlugin = function() {
}
this.loaded = true;
if (this.shouldInterceptLogs) {
this.interceptLogs(this.pluginModule);
this.setUpLogger();
}
(this.pluginModule.call(this, this));
};
@ -85,37 +102,6 @@ Plugin.prototype.pathToFile = function(filename) {
return utils.joinPath(this.pluginPath, filename);
};
Plugin.prototype.interceptLogs = function(context) {
var self = this;
// TODO: this is a bit nasty, figure out a better way
context.console = context.console || console;
//context.console.error = function(txt) {
// // TODO: logger should support an array instead of a single argument
// //self.logger.error.apply(self.logger, arguments);
// self.logger.error(self.name + " > " + txt);
//};
context.console.log = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.warn = function(txt) {
self.logger.warn(self.name + " > " + txt);
};
context.console.info = function(txt) {
self.logger.info(self.name + " > " + txt);
};
context.console.debug = function(txt) {
// TODO: ue JSON.stringify
self.logger.debug(self.name + " > " + txt);
};
context.console.trace = function(txt) {
self.logger.trace(self.name + " > " + txt);
};
context.console.dir = function(txt) {
self.logger.dir(txt);
};
};
// TODO: add deploy provider
Plugin.prototype.registerClientWeb3Provider = function(cb) {
this.clientWeb3Providers.push(cb);

View File

@ -77,8 +77,8 @@ class Dashboard {
this.screen.render();
}
logEntry(text) {
this.logText.log(text);
logEntry() {
this.logText.log(...arguments);
this.screen.render();
}

View File

@ -25,17 +25,9 @@ module.exports = function (embark) {
embark.registerActionForEvent("deploy:contract:beforeDeploy", (params, cb) => {
embark.logger.info("applying beforeDeploy plugin...");
//console.dir(params);
//console.dir(cb);
//console.dir('------------------');
cb();
});
// NOTE: uncommenting this will make dappConnection stop working
//embark.registerClientWeb3Provider(function(options) {
// return "web3 = new Web3(new Web3.providers.HttpProvider('http://" + options.rpcHost + ":" + options.rpcPort + "'));";
//});
embark.registerConsoleCommand((cmd) => {
if (cmd === "hello") {
return "hello there!";
@ -45,7 +37,7 @@ module.exports = function (embark) {
});
embark.events.on("contractsDeployed", function() {
embark.logger.info("plugin says: your contracts have been deployed");
embark.logger.info("plugin says:", ' your contracts have been deployed');
});
};