Skip to content

Commit e1a9023

Browse files
authored
Merge pull request #370 from embark-framework/feature/log-level-as-argument
Support setting log level as an argument
2 parents 849e34a + 09ca551 commit e1a9023

File tree

6 files changed

+62
-46
lines changed

6 files changed

+62
-46
lines changed

lib/cmd.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ class Cmd {
8787
build() {
8888
program
8989
.command('build [environment]')
90+
.option('--logfile [logfile]', 'filename to output logs (default: none)')
91+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
9092
.description('deploy and build dapp at dist/ (default: development)')
9193
.action(function (env, _options) {
92-
embark.build({env: env || 'development'});
94+
_options.env = env || 'development';
95+
_options.logFile = _options.logfile; // fix casing
96+
_options.logLevel = _options.loglevel; // fix casing
97+
embark.build(_options);
9398
});
9499
}
95100

@@ -102,6 +107,7 @@ class Cmd {
102107
.option('--nodashboard', 'simple mode, disables the dashboard')
103108
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
104109
.option('--logfile [logfile]', 'filename to output logs (default: none)')
110+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
105111
.description('run dapp (default: development)')
106112
.action(function (env, options) {
107113
embark.run({
@@ -110,7 +116,8 @@ class Cmd {
110116
serverHost: options.host,
111117
runWebserver: !options.noserver,
112118
useDashboard: !options.nodashboard,
113-
logfile: options.logfile
119+
logFile: options.logfile,
120+
logLevel: options.loglevel
114121
});
115122
});
116123
}
@@ -169,15 +176,18 @@ class Cmd {
169176

170177
upload() {
171178
program
172-
.command('upload [platform] [environment]')
179+
.command('upload <platform> [environment]')
173180
.option('--logfile [logfile]', 'filename to output logs (default: none)')
174-
.description('upload your dapp to a decentralized storage (e.g embark upload ipfs)')
181+
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
182+
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
175183
.action(function (platform, env, _options) {
176184
let environment = env || 'development';
177185
embark.initConfig(environment, {
178186
embarkConfig: 'embark.json', interceptLogs: false
179187
});
180188
_options.env = environment;
189+
_options.logFile = _options.logfile; // fix casing
190+
_options.logLevel = _options.loglevel; // fix casing
181191
embark.upload(platform, _options);
182192
});
183193
}
@@ -189,7 +199,7 @@ class Cmd {
189199
.action(function (env, options) {
190200
embark.graph({
191201
env: env || 'development',
192-
logfile: options.logfile
202+
logFile: options.logfile
193203
});
194204
});
195205
}

lib/core/engine.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ class Engine {
1616
this.embarkConfig = options.embarkConfig;
1717
this.interceptLogs = options.interceptLogs;
1818
this.version = options.version;
19-
this.logfile = options.logfile;
19+
this.logFile = options.logFile;
20+
this.logLevel = options.logLevel;
21+
this.events = options.events;
2022
}
2123

2224
init(_options) {
2325
let self = this;
2426
let options = _options || {};
25-
this.events = new Events();
26-
this.logger = options.logger || new Logger({logLevel: options.logLevel || 'debug', events: this.events, logfile: this.logfile});
27+
this.events = options.events || this.events || new Events();
28+
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
2729
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
2830
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
2931
this.plugins = this.config.plugins;

lib/core/logger.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class Logger {
77
this.logLevels = ['error', 'warn', 'info', 'debug', 'trace'];
88
this.logLevel = options.logLevel || 'info';
99
this.logFunction = options.logFunction || console.log;
10-
this.logfile = options.logfile;
10+
this.logFile = options.logFile;
1111
}
1212
}
1313

1414
Logger.prototype.writeToFile = function (txt) {
15-
if (!this.logfile) {
15+
if (!this.logFile) {
1616
return;
1717
}
1818

19-
fs.appendFileSync(this.logfile, "\n" + txt);
19+
fs.appendFileSync(this.logFile, "\n" + txt);
2020
};
2121

2222
Logger.prototype.error = function (txt) {

lib/index.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class Embark {
6161
env: options.env,
6262
version: this.version,
6363
embarkConfig: options.embarkConfig || 'embark.json',
64-
logfile: options.logfile
64+
logFile: options.logFile,
65+
logLevel: options.logLevel
6566
});
6667
engine.init();
6768

@@ -150,16 +151,20 @@ class Embark {
150151
});
151152
}
152153

153-
build(options, engine, continueProcessing) {
154-
if(!engine){
155-
engine = new Engine({
156-
env: options.env,
157-
version: this.version,
158-
embarkConfig: 'embark.json',
159-
interceptLogs: false
160-
});
161-
engine.init();
162-
}
154+
build(options, continueProcessing) {
155+
let engine = new Engine({
156+
env: options.env,
157+
version: this.version,
158+
embarkConfig: 'embark.json',
159+
interceptLogs: false,
160+
logFile: options.logFile,
161+
logLevel: options.logLevel,
162+
events: options.events,
163+
logger: options.logger,
164+
config: options.config,
165+
plugins: options.plugins
166+
});
167+
engine.init();
163168

164169
async.waterfall([
165170
function startServices(callback) {
@@ -207,7 +212,7 @@ class Embark {
207212
env: options.env,
208213
version: this.version,
209214
embarkConfig: options.embarkConfig || 'embark.json',
210-
logfile: options.logfile
215+
logFile: options.logFile
211216
});
212217
engine.init();
213218

@@ -254,61 +259,57 @@ class Embark {
254259

255260
upload(platform, options) {
256261

262+
// populate options that were instantiated with initConfig to pass around
257263
options.buildDir = 'dist/';
258264
options.storageConfig = this.config.storageConfig;
259-
260-
// initialise embark engine
261-
let engine = new Engine({
262-
env: options.env,
263-
version: this.version,
264-
embarkConfig: options.embarkConfig || 'embark.json',
265-
logfile: options.logfile
266-
});
267-
engine.init();
265+
options.events = this.events;
266+
options.logger = this.logger;
267+
options.config = this.config;
268268

269269
// load plugins
270270
this.plugins.loadInternalPlugin('ipfs', options);
271271
this.plugins.loadInternalPlugin('swarm', options);
272272

273-
let plugins = this.plugins;
273+
// upddate our options with loaded plugins
274+
options.plugins = this.plugins;
275+
274276
let cmdPlugin;
275277
let self = this;
276278
async.waterfall([
277279
function setupStoragePlugin(callback){
278280
// check use has input existing storage plugin
279-
let cmdPlugins = plugins.getPluginsFor('uploadCmds');
281+
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
280282

281283
if (cmdPlugins.length > 0) {
282284
cmdPlugin = cmdPlugins.find((pluginCmd) => {
283285
return pluginCmd.name == platform;
284286
});
285287
}
286288
if (!cmdPlugin) {
287-
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
289+
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
288290
callback({message: 'unknown platform: ' + platform});
289291
} else {
290292
callback();
291293
}
292294
},
293295
function buildAndDeployContracts(callback){
294296
// 2. upload to storage (outputDone event triggered after webpack finished)
295-
engine.events.on('outputDone', function () {
296-
engine.logger.info('deploying to ' + platform + '...');
297+
self.events.on('outputDone', function () {
297298
cmdPlugin.uploadCmds[0].cb()
298299
.then((success) => {
299300
callback(null, success);
300301
})
301302
.catch(callback);
302303
});
303304
// 1. build the contracts and dapp webpack
304-
self.build(options, engine, true);
305+
self.build(options, true);
305306
}
306307
], function (err, _result) {
307308
if (err) {
308-
engine.logger.error(err.message);
309-
engine.logger.debug(err.stack);
309+
self.logger.error(err.message);
310+
self.logger.debug(err.stack);
310311
} else {
311-
engine.logger.info("finished building dapp and deploying to " + platform.underline);
312+
self.logger.info("finished building dapp and deploying to " + platform.underline);
312313
}
313314

314315
// needed due to child processes

lib/modules/ipfs/upload.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class IPFS {
1313

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

lib/modules/swarm/upload.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Swarm {
1010

1111
deploy() {
1212
return new Promise((resolve, reject) => {
13+
console.log("deploying to swarm!");
1314
let self = this;
1415
async.waterfall([
1516
function findBinary(callback) {
@@ -25,8 +26,9 @@ class Swarm {
2526
function runCommand(swarm_bin, callback) {
2627
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
2728
console.log(("=== adding " + self.buildDir + " to swarm").green);
28-
console.log(cmd.green);
29-
shelljs.exec(cmd, function(code, stdout, stderr){
29+
console.trace(cmd);
30+
shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel
31+
console.log(stdout.green);
3032
callback(stderr, {code: code, output: stdout});
3133
});
3234
},

0 commit comments

Comments
 (0)