从打包的 asar 应用程序电子访问 extraFiles 电子生成器

时间:2021-03-02 17:13:36

标签: electron electron-builder winston

我有一个电子应用程序,它可以完美地运行到带有写入文件的日志引擎的开发环境中(使用 winston.js)。

项目的结构是这样的 :

enter image description here

当我运行电子构建器来打包我的应用程序时,我有这样的结构 :

enter image description here

  • 我的 app.asar 文件在资源目录中
  • 我的日志文件在数据/日志中

在开发环境中,我使用此 winston 配置访问日志文件 :

new winston.transports.File({filename: 'data/logs/error.log', level: 'error'})

但是这个特定的路径配置只运行到开发模式。而且我没有找到在生产环境中定位日志文件的好方法。

我需要在生产环境中使用什么样的路径来定位资源目录(或已安装应用程序的目录目录的根目录)之外的文件?

使用电子应用管理日志的最佳实践是什么?

1 个答案:

答案 0 :(得分:1)

要获取 electron-builder docs 中所述的打包应用程序中的资源和 minsplit = 1 路径,您可以使用 process.resourcesPath

extraFiles

对于日志,Electron 提供了用于此目的的路径,您可以使用 app.getPath

检查
const getResourcesPath = () => {
  return process.resourcesPath;
};

const getExtraFilesPath = () => {
  return path.join(process.resourcesPath, '..'); // go up one directory
};

默认情况下,这在 macOS 上为 app.getPath('logs'); ,在 Linux 和 Windows 上为 ~/Library/Logs/YourAppName 目录

如果需要,您可以使用 app.setAppLogsPath 更改此默认路径

大多数 Electron 应用将日志存储在默认位置