問題描述
在開發過程中如何在 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/.
這是一個包含 openDevTools 的簡單 main.js 文件:
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 中的錯誤消息和控制臺日志?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!