Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging doesn't work in programmatic use code example #11106

Closed
staceytay opened this issue Jul 15, 2020 · 2 comments
Closed

Logging doesn't work in programmatic use code example #11106

staceytay opened this issue Jul 15, 2020 · 2 comments

Comments

@staceytay
Copy link
Contributor

Logging doesn't output expected log level logs in the example

const fs = require('fs');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const log = require('lighthouse-logger');

(async () => {
  log.setLevel('info');
  const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
  const options = {output: 'html', onlyCategories: ['performance'], port: chrome.port};
  const runnerResult = await lighthouse('https://example.com', options);

  // `.report` is the HTML report as a string
  const reportHtml = runnerResult.report;
  fs.writeFileSync('lhreport.html', reportHtml);

  // `.lhr` is the Lighthouse Result as a JS object
  console.log('Report is done for', runnerResult.lhr.finalUrl);
  console.log('Performance score was', runnerResult.lhr.categories.performance.score * 100);

  await chrome.kill();
})();

from https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically.

Provide the steps to reproduce

  1. Run the code above using the lighthouse CLI.

What is the current behavior?

Output from running the sample code above:

Report is done for https://example.com/
Performance score was 100

What is the expected behavior?

  status Connecting to browser +0ms
  status Resetting state with about:blank +25ms
  status Benchmarking machine +104ms
  status Initializing… +504ms
  /* ...truncated... */
  status Auditing: Uses passive listeners to improve scrolling performance +13ms
  status Generating results... +0ms
Report is done for https://example.com/
Performance score was 100

Environment Information

  • Affected Channels: CLI
  • Lighthouse version: 6.1.1
  • Chrome version: NA
  • Node.js version: v12.16.3
  • Operating System: macOS

Related issues
#10788

Proposed Changes

  1. Update the example code snippet above to pass in the logLevel for Flags and remove the use of lighthouse-logger.
onst chromeLauncher = require('chrome-launcher');
-const log = require('lighthouse-logger');
 
 (async () => {
-  log.setLevel('info');
   const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
-  const options = {output: 'html', onlyCategories: ['performance'], port: chrome.port};
+  const options = {logLevel: 'info', output: 'html', onlyCategories: ['performance'], port: chrome.port};
   const runnerResult = await lighthouse('https://example.com', options);
  1. Update the Turn on logging section in the same doc.
-If you want to see log output as Lighthouse runs, include the `lighthouse-logger` module
-and set an appropriate logging level in your code. You'll also need to pass
+If you want to see log output as Lighthouse runs, set an appropriate logging level in your code and pass
 the `logLevel` flag when calling `lighthouse`.
 
 ```javascript
-const log = require('lighthouse-logger');
-
 const flags = {logLevel: 'info'};
-log.setLevel(flags.logLevel);
 
 launchChromeAndRunLighthouse('https://example.com', flags).then(...);

If the proposed changes above are ok, I'd be happy to submit a PR to fix them :)

@patrickhulce
Copy link
Collaborator

Thanks @staceytay those changes would be very welcome in a PR! :)

@staceytay
Copy link
Contributor Author

Closing since fixed in #11116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants