在Electron中出现错误消息和控制台日志? [英] Error messages and console logs in Electron?

查看:994
本文介绍了在Electron中出现错误消息和控制台日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发过程中如何查看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?

编辑:类似于Chrome开发工具显示的错误和控制台日志:

除了Electron而不是Chrome。

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

推荐答案

在BrowserWindow上调用函数 openDevTools(),这将打开与您相同的开发工具在Chrome中找到。我在我的博客> http://www.mylifeforthecode.com/debugging-renderer-process上写过-in-electron /

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

这是一个简单的main.js文件,其中包含openDevTools:

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;
  });  
});

您也可以使用远程模块通过渲染器进程来访问它。对于我一直在修改的应用程序,我将功能 toggleDevTools 绑定到F12。像这样:

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();         
    }
  });

请注意,我仅在Windows中使用Electron测试了上述内容。我假设Linux和Mac版本工作相同。如果您使用的是Mac或Linux,请告诉我它们是否没有。

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.

这篇关于在Electron中出现错误消息和控制台日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆