web-ext icon indicating copy to clipboard operation
web-ext copied to clipboard

Processing Chrome's extension console log from the outside?

Open matanox opened this issue 3 years ago • 8 comments

Hi,

I find that processing the extension's logs would be of great value for CI and (headless) tests, aside being a great time saver for quickly figuring very immediately whenever your extension crashes while loading, during regular extension development.

I have tried --browser-console but it has no effect in my case where the target binary is the current Chrome Beta channel Chrome version 93 release. I would also like to eventually process the extension's console log via the command line, if such an option is available, for testing/CI and for early warnings upon source change reloads.

Chromium has a myriad of command-line startup options relating to debugging and logging levels of course, and it has a remote logging API ― they can obviously be added to the target binary via the --args option of web-ext, but I wonder if you have any kind or built-in feature (or recipe) for particularly getting the extension's console log for processing outside the browser UI; The background service worker's console being or particular interest in my view and case.

My setup: Ubuntu 20.04, latest npm install of web-ext.

My sincere thank you for your thoughts and assistance, Matan

matanox avatar Aug 08 '21 14:08 matanox

web-ext does currently not offer special support for directing output from Chrome to stderr/stdout. The --verbose flag of web-ext isn't hooked up, but we could do that if it's useful.

Chrome does support a way to globally enable logging of internals (https://www.chromium.org/for-testers/enable-logging). It can be used to direct the output of the JS console to stderr or a file, but it doesn't offer the ability to do this for a specific tab in a reasonable way (technically it may be possible to do so with --renderer-cmd-prefix, but that may have other side effects).

If we are willing to dump the console of Chrome (regardless of source) to the output when --verbose is passed to web-ext, then we would have to:

  • Forward Chrome's stderr (and maybe stdout) to web-ext's stderr instead of ignoring it (like we do right now).
  • Pass the following parameters to Chromium: --enable-logging=stderr --vmodule=console=1

Rob--W avatar Aug 09 '21 12:08 Rob--W

The chrome-launcher dependency doesn't expose the ability to redirect stdout/stderr:

  • https://github.com/GoogleChrome/chrome-launcher/issues/96
  • https://github.com/GoogleChrome/chrome-launcher/blob/b00fa22f94371f6d38d2d23e6e0b32fc7af470f0/src/chrome-launcher.ts#L259

Chrome does unconditionally write to chrome-out.log and chrome-err.log (https://github.com/GoogleChrome/chrome-launcher/blob/b00fa22f94371f6d38d2d23e6e0b32fc7af470f0/src/chrome-launcher.ts#L197-L198), which you could read (in conjunction with the flags I mentioned above).

We cannot implement the feature requested here unless Chrome and/or chrome-launcher offers support for capturing specific logs and stderr/stdout.

Rob--W avatar Aug 19 '21 13:08 Rob--W

Thanks for the research!

matanox avatar Aug 24 '21 19:08 matanox

I can't say I can figure how to get chromium/chrome to provide the console log of a particular service worker either. Might chase that later and come back here.

matanox avatar Aug 24 '21 22:08 matanox

Is this a Chrome-specific issue? I don't see any stdout when running Firefox either.

I'm running tape in the content script and background page but currently I have to manually open each context to inspect the logs.

I'd love to see the console in the terminal. However I'm not too sure how I'd ask web-ext to close when the tests are done 😕

Screen Shot 8

fregante avatar Sep 27 '21 12:09 fregante

When --verbose is set, output from Firefox is forwarded to the output.

There are various ways to close web-ext. For example by using it as a library. Or by starting a local server, and send a request to that server from the test. Upon receiving the message, send a SIGINT signal to the web-ext process.

Rob--W avatar Oct 01 '21 13:10 Rob--W

When --verbose is set, output from Firefox is forwarded to the output.

What's "output from Firefox"? It's not the console I screenshotted, as far as I can see, so this is not a Chrome-specific issue.

fregante avatar Oct 01 '21 14:10 fregante

When --verbose is set, output from Firefox is forwarded to the output.

What's "output from Firefox"? It's not the console I screenshotted, as far as I can see, so this is not a Chrome-specific issue.

I see messages from the global browser console in the output. Things like console.log and errors from background script are not visible (but there may be a pref or environment variable (possibly MOZ_LOG) to get the log to appear too).

If you really like to output something without being drowned in logspam in stdout, then you can use dump("message here\n"); to print messages that are visible when --verbose is used with web-ext. web-ext run sets the browser.dom.window.dump.enabled preference to true, which adds the dump function to the scripting environment (including those in extensions and web pages).

Rob--W avatar Oct 01 '21 16:10 Rob--W