Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using console.log() in Electron app

Tags:

electron

People also ask

How do you debug an Electron app?

You can start your Electron application in debug mode using the --debug flag, which will—by default—enable remote debugging on port 5858. Visual Studio Code is a free, open-source IDE available for Windows, Linux, and macOS and has been—coincidentally—built on top of Electron by Microsoft.

Is Electron app fast?

Super Fast TurnaroundElectron's number one strength is its turnaround speed. No other application development framework can go from 0 to fully functioning app as quickly as Electron can. Recently we were able to turnaround an app for a client in 2 weeks, because it was built on top of an existing React library.


console.log works, but where it logs to depends on whether you call it from the main process or the renderer process.

If you call it from the renderer process (i.e. JavaScript that is included from your index.html file) it will be logged to the dev tools window.

If you call it from the main process (i.e. in main.js) it will work the same way as it does in Node - it will log to the terminal window. If you're starting your Electron process from the Terminal using electron . you can see your console.log calls from the main process there.


You can also add an environment variable in windows:

ELECTRON_ENABLE_LOGGING=1

This will output console messages to your terminal.


There is another way of logging to the console from inside the renderer process. Given this is Electron, you can access Node's native modules. This includes the console module.

var nodeConsole = require('console');
var myConsole = new nodeConsole.Console(process.stdout, process.stderr);
myConsole.log('Hello World!');

When this code is run from inside the renderer process, you will get Hello World! in the terminal you ran Electron from.

See https://nodejs.org/api/console.html for further documentation on the console module.


Yet another possibility is accessing the main process console using remote.getGlobal(name):

const con = require('electron').remote.getGlobal('console')
con.log('This will be output to the main process console.')

Adding to M. Damian's answer, here's how I set it up so I could access the main process's console from any renderer.

In your main app, add:

const electron = require('electron');
const app = electron.app;
const console = require('console');
...
app.console = new console.Console(process.stdout, process.stderr);

In any renderer, you can add:

const remote = require('electron').remote;
const app = remote.app;
...
app.console.log('This will output to the main process console.');

process.stdout.write('your output to command prompt console or node js ')

You can use the npm package electron-log https://www.npmjs.com/package/electron-log

It will log your error, warn, info, verbose, debug, silly outputs in your native os log.

var log = require('electron-log');

log.info('Hello, log');
log.error('Damn it, an error');