简体   繁体   English

Electron 中的错误消息和控制台日志?

[英]Error messages and console logs in Electron?

How do you view error messages and console logs in Electron during development?在开发过程中如何查看 Electron 中的错误消息和控制台日志? 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:编辑:有点像 Chrome 开发工具显示的错误和控制台日志:Chrome 开发工具的屏幕截图 Except in Electron rather than Chrome.除了 Electron 而不是 Chrome。

On your BrowserWindow call the function openDevTools() this will open the same dev tools you find in Chrome.在您的 BrowserWindow 上调用函数openDevTools()这将打开您在 Chrome 中找到的相同开发工具。 I wrote about this on my blog at http://www.mylifeforthecode.com/debugging-renderer-process-in-electron/ .我在我的博客http://www.mylifeforthecode.com/debugging-renderer-process-in-electron/上写了这个。

Here is a simple main.js file that includes openDevTools:这是一个包含 openDevTools 的简单 main.js 文件:

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.对于我一直在修补的应用程序,我将功能toggleDevTools绑定到 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.请注意,我仅在 Windows 中使用 Electron 测试了上述内容。 I am assuming the Linux and Mac versions work the same.我假设 Linux 和 Mac 版本的工作方式相同。 If you are running Mac or Linux please let me know if they do not.如果您运行的是 Mac 或 Linux,请让我知道他们是否不运行。

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我在 Windows 上使用 Electron

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM