Merge pull request #370 from embark-framework/feature/log-level-as-argument

Support setting log level as an argument
This commit is contained in:
Iuri Matias 2018-04-19 13:03:00 -04:00 committed by GitHub
commit e1a9023bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 46 deletions

View File

@ -87,9 +87,14 @@ class Cmd {
build() { build() {
program program
.command('build [environment]') .command('build [environment]')
.option('--logfile [logfile]', 'filename to output logs (default: none)')
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.description('deploy and build dapp at dist/ (default: development)') .description('deploy and build dapp at dist/ (default: development)')
.action(function (env, _options) { .action(function (env, _options) {
embark.build({env: env || 'development'}); _options.env = env || 'development';
_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
embark.build(_options);
}); });
} }
@ -102,6 +107,7 @@ class Cmd {
.option('--nodashboard', 'simple mode, disables the dashboard') .option('--nodashboard', 'simple mode, disables the dashboard')
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes') .option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
.option('--logfile [logfile]', 'filename to output logs (default: none)') .option('--logfile [logfile]', 'filename to output logs (default: none)')
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.description('run dapp (default: development)') .description('run dapp (default: development)')
.action(function (env, options) { .action(function (env, options) {
embark.run({ embark.run({
@ -110,7 +116,8 @@ class Cmd {
serverHost: options.host, serverHost: options.host,
runWebserver: !options.noserver, runWebserver: !options.noserver,
useDashboard: !options.nodashboard, useDashboard: !options.nodashboard,
logfile: options.logfile logFile: options.logfile,
logLevel: options.loglevel
}); });
}); });
} }
@ -169,15 +176,18 @@ class Cmd {
upload() { upload() {
program program
.command('upload [platform] [environment]') .command('upload <platform> [environment]')
.option('--logfile [logfile]', 'filename to output logs (default: none)') .option('--logfile [logfile]', 'filename to output logs (default: none)')
.description('upload your dapp to a decentralized storage (e.g embark upload ipfs)') .option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
.action(function (platform, env, _options) { .action(function (platform, env, _options) {
let environment = env || 'development'; let environment = env || 'development';
embark.initConfig(environment, { embark.initConfig(environment, {
embarkConfig: 'embark.json', interceptLogs: false embarkConfig: 'embark.json', interceptLogs: false
}); });
_options.env = environment; _options.env = environment;
_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
embark.upload(platform, _options); embark.upload(platform, _options);
}); });
} }
@ -189,7 +199,7 @@ class Cmd {
.action(function (env, options) { .action(function (env, options) {
embark.graph({ embark.graph({
env: env || 'development', env: env || 'development',
logfile: options.logfile logFile: options.logfile
}); });
}); });
} }

View File

@ -16,14 +16,16 @@ class Engine {
this.embarkConfig = options.embarkConfig; this.embarkConfig = options.embarkConfig;
this.interceptLogs = options.interceptLogs; this.interceptLogs = options.interceptLogs;
this.version = options.version; this.version = options.version;
this.logfile = options.logfile; this.logFile = options.logFile;
this.logLevel = options.logLevel;
this.events = options.events;
} }
init(_options) { init(_options) {
let self = this; let self = this;
let options = _options || {}; let options = _options || {};
this.events = new Events(); this.events = options.events || this.events || new Events();
this.logger = options.logger || new Logger({logLevel: options.logLevel || 'debug', events: this.events, logfile: this.logfile}); this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
this.config = new Config({env: this.env, logger: this.logger, events: this.events}); this.config = new Config({env: this.env, logger: this.logger, events: this.events});
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs}); this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
this.plugins = this.config.plugins; this.plugins = this.config.plugins;

View File

@ -7,16 +7,16 @@ class Logger {
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace']; this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
this.logLevel = options.logLevel || 'info'; this.logLevel = options.logLevel || 'info';
this.logFunction = options.logFunction || console.log; this.logFunction = options.logFunction || console.log;
this.logfile = options.logfile; this.logFile = options.logFile;
} }
} }
Logger.prototype.writeToFile = function (txt) { Logger.prototype.writeToFile = function (txt) {
if (!this.logfile) { if (!this.logFile) {
return; return;
} }
fs.appendFileSync(this.logfile, "\n" + txt); fs.appendFileSync(this.logFile, "\n" + txt);
}; };
Logger.prototype.error = function (txt) { Logger.prototype.error = function (txt) {

View File

@ -61,7 +61,8 @@ class Embark {
env: options.env, env: options.env,
version: this.version, version: this.version,
embarkConfig: options.embarkConfig || 'embark.json', embarkConfig: options.embarkConfig || 'embark.json',
logfile: options.logfile logFile: options.logFile,
logLevel: options.logLevel
}); });
engine.init(); engine.init();
@ -150,16 +151,20 @@ class Embark {
}); });
} }
build(options, engine, continueProcessing) { build(options, continueProcessing) {
if(!engine){ let engine = new Engine({
engine = new Engine({ env: options.env,
env: options.env, version: this.version,
version: this.version, embarkConfig: 'embark.json',
embarkConfig: 'embark.json', interceptLogs: false,
interceptLogs: false logFile: options.logFile,
}); logLevel: options.logLevel,
engine.init(); events: options.events,
} logger: options.logger,
config: options.config,
plugins: options.plugins
});
engine.init();
async.waterfall([ async.waterfall([
function startServices(callback) { function startServices(callback) {
@ -207,7 +212,7 @@ class Embark {
env: options.env, env: options.env,
version: this.version, version: this.version,
embarkConfig: options.embarkConfig || 'embark.json', embarkConfig: options.embarkConfig || 'embark.json',
logfile: options.logfile logFile: options.logFile
}); });
engine.init(); engine.init();
@ -254,29 +259,26 @@ class Embark {
upload(platform, options) { upload(platform, options) {
// populate options that were instantiated with initConfig to pass around
options.buildDir = 'dist/'; options.buildDir = 'dist/';
options.storageConfig = this.config.storageConfig; options.storageConfig = this.config.storageConfig;
options.events = this.events;
// initialise embark engine options.logger = this.logger;
let engine = new Engine({ options.config = this.config;
env: options.env,
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logfile: options.logfile
});
engine.init();
// load plugins // load plugins
this.plugins.loadInternalPlugin('ipfs', options); this.plugins.loadInternalPlugin('ipfs', options);
this.plugins.loadInternalPlugin('swarm', options); this.plugins.loadInternalPlugin('swarm', options);
let plugins = this.plugins; // upddate our options with loaded plugins
options.plugins = this.plugins;
let cmdPlugin; let cmdPlugin;
let self = this; let self = this;
async.waterfall([ async.waterfall([
function setupStoragePlugin(callback){ function setupStoragePlugin(callback){
// check use has input existing storage plugin // check use has input existing storage plugin
let cmdPlugins = plugins.getPluginsFor('uploadCmds'); let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
if (cmdPlugins.length > 0) { if (cmdPlugins.length > 0) {
cmdPlugin = cmdPlugins.find((pluginCmd) => { cmdPlugin = cmdPlugins.find((pluginCmd) => {
@ -284,7 +286,7 @@ class Embark {
}); });
} }
if (!cmdPlugin) { if (!cmdPlugin) {
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green); self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
callback({message: 'unknown platform: ' + platform}); callback({message: 'unknown platform: ' + platform});
} else { } else {
callback(); callback();
@ -292,8 +294,7 @@ class Embark {
}, },
function buildAndDeployContracts(callback){ function buildAndDeployContracts(callback){
// 2. upload to storage (outputDone event triggered after webpack finished) // 2. upload to storage (outputDone event triggered after webpack finished)
engine.events.on('outputDone', function () { self.events.on('outputDone', function () {
engine.logger.info('deploying to ' + platform + '...');
cmdPlugin.uploadCmds[0].cb() cmdPlugin.uploadCmds[0].cb()
.then((success) => { .then((success) => {
callback(null, success); callback(null, success);
@ -301,14 +302,14 @@ class Embark {
.catch(callback); .catch(callback);
}); });
// 1. build the contracts and dapp webpack // 1. build the contracts and dapp webpack
self.build(options, engine, true); self.build(options, true);
} }
], function (err, _result) { ], function (err, _result) {
if (err) { if (err) {
engine.logger.error(err.message); self.logger.error(err.message);
engine.logger.debug(err.stack); self.logger.debug(err.stack);
} else { } else {
engine.logger.info("finished building dapp and deploying to " + platform.underline); self.logger.info("finished building dapp and deploying to " + platform.underline);
} }
// needed due to child processes // needed due to child processes

View File

@ -13,7 +13,7 @@ class IPFS {
deploy() { deploy() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log("deploying!"); console.log("deploying to ipfs!");
let self = this; let self = this;
async.waterfall([ async.waterfall([
function findBinary(callback) { function findBinary(callback) {
@ -29,8 +29,9 @@ class IPFS {
function runCommand(ipfs_bin, callback) { function runCommand(ipfs_bin, callback) {
let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`;
console.log(("=== adding " + self.buildDir + " to ipfs").green); console.log(("=== adding " + self.buildDir + " to ipfs").green);
console.log(cmd.green); console.trace(cmd);
shelljs.exec(cmd, function(code, stdout, stderr){ shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
console.log(stdout.green);
callback(stderr, stdout); callback(stderr, stdout);
}); });
}, },

View File

@ -10,6 +10,7 @@ class Swarm {
deploy() { deploy() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log("deploying to swarm!");
let self = this; let self = this;
async.waterfall([ async.waterfall([
function findBinary(callback) { function findBinary(callback) {
@ -25,8 +26,9 @@ class Swarm {
function runCommand(swarm_bin, callback) { function runCommand(swarm_bin, callback) {
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`; let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
console.log(("=== adding " + self.buildDir + " to swarm").green); console.log(("=== adding " + self.buildDir + " to swarm").green);
console.log(cmd.green); console.trace(cmd);
shelljs.exec(cmd, function(code, stdout, stderr){ shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
console.log(stdout.green);
callback(stderr, {code: code, output: stdout}); callback(stderr, {code: code, output: stdout});
}); });
}, },