Skip to content

Commit 6663f6b

Browse files
paulirishbrendankenny
authored andcommitted
Clean up CLI logging, moving protocol work to --verbose. (#556)
1 parent 699eafd commit 6663f6b

File tree

8 files changed

+33
-39
lines changed

8 files changed

+33
-39
lines changed

lighthouse-cli/index.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ if (!environment.checkNodeCompatibility()) {
2727
const yargs = require('yargs');
2828
const Printer = require('./printer');
2929
const lighthouse = require('../lighthouse-core');
30-
const log = require('../lighthouse-core/lib/log');
3130

3231
const cli = yargs
3332
.help('help')
@@ -127,7 +126,7 @@ if (cli.listTraceCategories) {
127126

128127
const url = cli._[0];
129128
const outputMode = cli.output;
130-
const outputPath = cli.outputPath;
129+
const outputPath = cli['output-path'];
131130
const flags = cli;
132131
const config = (cli.configPath && require(cli.configPath)) || null;
133132

@@ -146,25 +145,6 @@ if (!flags.auditWhitelist || flags.auditWhitelist === 'all') {
146145
flags.auditWhitelist = new Set(flags.auditWhitelist.split(',').map(a => a.toLowerCase()));
147146
}
148147

149-
// Listen on progress events, record their start timestamps
150-
// and print result using the logger.
151-
let timers = {};
152-
log.events.on('status', function(event) {
153-
let msg = event[1];
154-
if (!msg) {
155-
return;
156-
}
157-
timers[msg] = Date.now();
158-
});
159-
log.events.on('statusEnd', function(event) {
160-
let msg = event[1];
161-
if (!msg) {
162-
return;
163-
}
164-
let t = Date.now() - timers[msg];
165-
log.log('Timer', `${msg} ${t}ms`);
166-
});
167-
168148
// kick off a lighthouse run
169149
lighthouse(url, flags, config)
170150
.then(results => Printer.write(results, outputMode, outputPath))

lighthouse-cli/printer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ function createOutput(results, outputMode) {
130130
* @return {!Promise}
131131
*/
132132
function writeToStdout(output) {
133-
return Promise.resolve(process.stdout.write(`${output}\n`));
133+
return new Promise((resolve, reject) => {
134+
// small delay to avoid race with debug() logs
135+
setTimeout(_ => {
136+
process.stdout.write(`${output}\n`);
137+
resolve();
138+
}, 50);
139+
});
134140
}
135141

136142
/**

lighthouse-core/gather/drivers/cri.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ class CriDriver extends Driver {
105105
}
106106

107107
return new Promise((resolve, reject) => {
108-
this.formattedLog('method => browser', {method: command, params: params});
108+
this.formattedLog('method => browser', {method: command, params: params}, 'verbose');
109109

110110
this._cri.send(command, params, (err, result) => {
111111
if (err) {
112112
this.formattedLog('method <= browser ERR', {method: command, params: result}, 'error');
113113
return reject(result);
114114
}
115-
this.formattedLog('method <= browser OK', {method: command, params: result});
115+
this.formattedLog('method <= browser OK', {method: command, params: result}, 'verbose');
116116
resolve(result);
117117
});
118118
});

lighthouse-core/gather/drivers/driver.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ class Driver {
7575
formattedLog(prefix, data, level) {
7676
const columns = (!process || process.browser) ? Infinity : process.stdout.columns;
7777
const maxLength = columns - data.method.length - prefix.length - 18;
78-
const snippet = data.params ? JSON.stringify(data.params).substr(0, maxLength) : '';
78+
// IO.read blacklisted here to avoid logging megabytes of trace data
79+
const snippet = (data.params && data.method !== 'IO.read') ?
80+
JSON.stringify(data.params).substr(0, maxLength) : '';
7981
log[level ? level : 'log'](prefix, data.method, snippet);
8082
}
8183

@@ -101,7 +103,7 @@ class Driver {
101103
}
102104

103105
// log event listeners being bound
104-
this.formattedLog('listen for event =>', {method: eventName});
106+
this.formattedLog('listen for event =>', {method: eventName}, 'verbose');
105107
this._eventEmitter.on(eventName, cb);
106108
}
107109

@@ -116,7 +118,7 @@ class Driver {
116118
throw new Error('connect() must be called before attempting to listen to events.');
117119
}
118120
// log event listeners being bound
119-
this.formattedLog('listen once for event =>', {method: eventName});
121+
this.formattedLog('listen once for event =>', {method: eventName}, 'verbose');
120122
this._eventEmitter.once(eventName, cb);
121123
}
122124

lighthouse-core/gather/drivers/extension.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ExtensionDriver extends Driver {
109109
*/
110110
sendCommand(command, params) {
111111
return new Promise((resolve, reject) => {
112-
this.formattedLog('method => browser', {method: command, params: params});
112+
this.formattedLog('method => browser', {method: command, params: params}, 'verbose');
113113
if (!this._tabId) {
114114
log.error('No tabId set for sendCommand');
115115
}
@@ -124,7 +124,7 @@ class ExtensionDriver extends Driver {
124124
return reject(result.exceptionDetails);
125125
}
126126

127-
this.formattedLog('method <= browser OK', {method: command, params: result});
127+
this.formattedLog('method <= browser OK', {method: command, params: result}, 'verbose');
128128
resolve(result);
129129
});
130130
});

lighthouse-core/gather/gather-runner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class GatherRunner {
119119
log.log('status', `Gathering: trace "${traceName}"`);
120120
return driver.endTrace().then(traceContents => {
121121
loadData.traces[traceName] = {traceContents};
122-
log.log('statusEnd', `Gathering: trace "${traceName}"`);
122+
log.verbose('statusEnd', `Gathering: trace "${traceName}"`);
123123
});
124124
});
125125
}
@@ -130,7 +130,7 @@ class GatherRunner {
130130
log.log('status', status);
131131
return driver.endNetworkCollect().then(networkRecords => {
132132
loadData.networkRecords = networkRecords;
133-
log.log('statusEnd', status);
133+
log.verbose('statusEnd', status);
134134
});
135135
});
136136
}
@@ -144,7 +144,7 @@ class GatherRunner {
144144
loadData.traceContents = loadData.traces[traceName].traceContents;
145145
}
146146
return Promise.resolve(gatherer.afterPass(options, loadData)).then(ret => {
147-
log.log('statusEnd', status);
147+
log.verbose('statusEnd', status);
148148
return ret;
149149
});
150150
});

lighthouse-core/lib/log.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,21 @@ function _log(title, logargs) {
3838
return loggers[title](...args);
3939
}
4040

41-
class Emitter extends EventEmitter { }
42-
const events = new Emitter();
41+
class Emitter extends EventEmitter {
42+
// issueStatus fires off all status updates
43+
// listen with `require('lib/log').events.addListener('status', callback)`
44+
issueStatus(title, args) {
45+
if (title === 'status' || title === 'statusEnd') {
46+
this.emit(title, args);
47+
}
48+
}
49+
}
4350

4451
module.exports = {
4552
setLevel,
46-
events,
53+
events: new Emitter(),
4754
log(title) {
48-
if (title === 'status' || title === 'statusEnd') {
49-
events.emit(title, arguments);
50-
}
55+
this.events.issueStatus(title, arguments);
5156
return _log(title, arguments);
5257
},
5358

@@ -60,6 +65,7 @@ module.exports = {
6065
},
6166

6267
verbose(title) {
68+
this.events.issueStatus(title, arguments);
6369
return _log(`${title}:verbose`, arguments);
6470
}
6571
};

lighthouse-core/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Runner {
6868
const status = `Evaluating: ${audit.meta.description}`;
6969
log.log('status', status);
7070
return Promise.resolve(audit.audit(artifacts)).then(ret => {
71-
log.log('statusEnd', status);
71+
log.verbose('statusEnd', status);
7272
return ret;
7373
});
7474
})));

0 commit comments

Comments
 (0)