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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30814336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:43:42  来源:igfitidea点击:

Error messages and console logs in Electron?

javascriptnode.jsdebuggingelectron

提问by Oztaco - Reinstate Monica C.

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?

在开发过程中如何在 Electron 中查看错误消息和控制台日志?另外,是否可以将日志直接写入文件?



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

编辑:有点像 Chrome 的开发工具显示的错误和控制台日志: Chrome 开发工具的屏幕截图除了 Electron 而不是 Chrome。

回答by Shawn Rakowski

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/.

在您的 BrowserWindow 调用该函数,openDevTools()这将打开您在 Chrome 中找到的相同开发工具。我在我的博客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 toggleDevToolsto F12. Something like this:

您还可以使用远程模块通过渲染器进程访问它。对于我一直在修补的应用程序,我将函数绑定toggleDevTools到 F12。像这样的东西:

  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.

请注意,我仅在 Windows 中使用 Electron 测试了上述内容。我假设 Linux 和 Mac 版本的工作方式相同。如果您运行的是 Mac 或 Linux,请让我知道他们是否运行。

回答by Adam Kuzański

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

以前的答案今天有点过时,但几乎完美。

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

mainWindow = new BrowserWindow({width: 800, height: 600}); 主窗口。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