Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error messages and console logs in Electron?

How do you view error messages and console logs in Electron during development? Also, is it possible for the logs to be written directly into a file?


Edit: Kind of like the errors and console logs displayed by Chrome's dev tools: Screenshot of Chrome's dev tools Except in Electron rather than Chrome.

like image 622
Oztaco Avatar asked Jun 13 '15 01:06

Oztaco


People also ask

What does console log () do?

The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

Where are Electron logs stored?

on macOS: ~/Library/Logs/{app name}/{process type}. log. on Windows: %USERPROFILE%\AppData\Roaming\{app name}\logs\{process type}.

How do you debug an Electron main process?

The DevTools in an Electron browser window can only debug JavaScript that's executed in that window (i.e. the web pages). To debug JavaScript that's executed in the main process you will need to use an external debugger and launch Electron with the --inspect or --inspect-brk switch.


2 Answers

On your BrowserWindow call the function openDevTools() this will open the same dev tools you find in Chrome. I wrote about this on my blog at http://www.mylifeforthecode.com/debugging-renderer-process-in-electron/.

Here is a simple main.js file that includes openDevTools:

var app = require('app');
var BrowserWindow = require('browser-window');

var mainWindow = null;

app.on('window-all-closed', function() {
  if (process.platform != 'darwin')  
    app.quit();
});

app.on('ready', function() {    
  mainWindow = new BrowserWindow({width: 800, height: 600});  
  mainWindow.loadUrl('file://' + __dirname + '/index.html');
  mainWindow.openDevTools();
  mainWindow.on('closed', function() {
    mainWindow = null;
  });  
});

You can also access this via a renderer process using the remote module. For the apps I have been tinkering with I bind the function toggleDevTools to F12. Something like this:

  var remote = require('remote');           
  document.addEventListener("keydown", function (e) {  
    if (e.keyCode === 123) { // F12
      var window = remote.getCurrentWindow();
      window.toggleDevTools();         
    }
  });

Note that I have only tested the above with Electron in Windows. I am assuming the Linux and Mac versions work the same. If you are running Mac or Linux please let me know if they do not.

like image 124
Shawn Rakowski Avatar answered Sep 20 '22 09:09

Shawn Rakowski


The previous answer is a bit outdated today, but almost perfect.

mainWindow = new BrowserWindow({width: 800, height: 600}); 
mainWindow.webContents.openDevTools();

It opens dev tools automatically when app is running in electron. I am using Electron on Windows

Source https://electronjs.org/docs/tutorial/application-debugging

like image 43
Adam Kuzański Avatar answered Sep 20 '22 09:09

Adam Kuzański