-1

Is there a way, in Linux, to make the electron executable copy or redirect output sent to its console to the shell console, i.e. when I run it from a terminal?

For instance, if I run, in a shell, electron foo.js, where foo.js contains simply console.log("foo"), I get the foo message echoed back to my terminal.

However, when I run a React app, the output of console.log is directed to the browser's console (as explained here).

Is there a way to copy/redirect the outputs sent to the browser Console, so that they will also be sent to the shell console? This can be very useful for some kinds of debugging (e.g. using grep and regexes).

Alternatively, if there are other methods than console.log that allow writing to the shell which created the browser process, that would also be helpful.

Edit: @0stone0 mentioned the difference between server code and client code; from the point of view of debugging, since I am running a local process, the browser could very well decide to copy its console output to stdout/stderr. I fail to see why technically Chromium could not do it; requiring an API or websockets for this use case seems excessive, but if that is indeed necessary (e.g. for security reasons), that is a valid answer for me.

anol
  • 8,264
  • 3
  • 34
  • 78

1 Answers1

0

I just found a way, thanks to this question:

Q: Does anyone know of a way to save the console.log output in Chrome to a file? Or how to copy the text out of the console?

A: Enable logging from the command line using the flags:

--enable-logging --v=1

I then combined it with the answer to this question to set the Chromium flag in my Electron app:

app.commandLine.appendSwitch('enable-logging');

And that is enough for me to get messages in my console, such as:

[1234:0328/3923.5981:INFO:CONSOLE(2)] "logging", source: file:///home/user/app/main.js
anol
  • 8,264
  • 3
  • 34
  • 78