Where can I find the logs for my Electron app in production?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 00:02:32

If you mean console from within the webapp, then this applies :)

You need to make a callback for this to work. Read more about them here: http://electron.atom.io/docs/api/remote/

Here is a short example:

In a file next to your electron main.js, named logger.js, add this code:

exports.log = (entry) => {
    console.log(entry);
}

And then in your webapp, use this to call this log method callback:

// This line gets the code from the newly created file logger.js
const logger = require('electron').remote.require('./logger');

// This line calls the function exports.log from the logger.js file, but
// this happens in the context of the electron app, so from here you can 
// see it in the console when running the electron app or write to disk.
logger.log('Woohoo!');

You might also want to have a look at https://www.npmjs.com/package/electron-log for "better" logging and writing to disk. But you always need to use callbacks.

Old question, but I found convenient to configure this in package.json (this applies to console from the main process)

 "main": "app/src/main.js",
  "scripts": {
    "postinstall": "install-app-deps",
    "start": "npm install && electron . > /tmp/electron-app.log",
    "pack": "build --dir",
    "dist": "build",
    "dist:win": "build --platform win32",
    "dist:linux": "build --platform linux"
  }

You might elaborate it a little, like getting the /tmp/ path from somewhere, but you get the idea :)

I would advice over the accepted answer, and in general about constantly calling "main" from the renderer: The overhead of this context change is rather big, and can even be huge if you log JSON objects that have to be stringified and parsed back in the trip. Your renderer will be running with a handbrake!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!