当前位置: 首页 > 文档资料 > Electron 中文文档 >

崩溃日志报告

优质
小牛编辑
130浏览
2023-12-01

将崩溃日志提交给远程服务器

进程: Main, Renderer

以下是一个自动提交崩溃日志到服务器的示例

const { crashReporter } = require('electron')
crashReporter.start({
  productName: 'YourName',
  companyName: 'YourCompany',
  submitURL: 'https://your-domain.com/url-to-submit',
  uploadToServer: true
})

构建一个用于接受和处理崩溃日志的服务,你需要以下工程

  • socorro
  • mini-breakpad-server

或者使用第三方托管的解决办法:

  • Backtrace I/O
  • Sentry

崩溃日志被保存在本地的应用特有的临时文件夹中。 对于YourName对象中的productName,崩溃报日志将被储存在临时文件夹中名为YourName Crashes的文件文件夹中。 在启动崩溃报告器之前,您可以通过调用app.setPath('temp', 'my/custom/temp') API来自定义这些临时文件的保存路径

crashReporter

Submit crash reports to a remote server.

Process: Main, Renderer

The following is an example of automatically submitting a crash report to a remote server:

const { crashReporter } = require('electron')
crashReporter.start({
  productName: 'YourName',
  companyName: 'YourCompany',
  submitURL: 'https://your-domain.com/url-to-submit',
  uploadToServer: true
})

For setting up a server to accept and process crash reports, you can use following projects:

  • socorro
  • mini-breakpad-server

Or use a 3rd party hosted solution:

  • Backtrace I/O
  • Sentry

Crash reports are saved locally in an application-specific temp directory folder. For a productName of YourName, crash reports will be stored in a folder named YourName Crashes inside the temp directory. You can customize this temp directory location for your app by calling the app.setPath('temp', '/my/custom/temp') API before starting the crash reporter.

方法

crashReporter 模块具有以下方法:

Methods

The crashReporter module has the following methods:

crashReporter.start(options)

  • options Object

    • compannyame 字符串
    • submitURL 字符串 - 崩溃日志将以POST的方式发送给此URL.
    • productName 字符串(可选) - 默认为 app.getName().
    • uploadToServer 布尔型(可选) - 控制是否将崩溃日志发送给服务器,默认为true.
    • ignoreSystemCrashHandler 布尔型(可选) - 默认为false.
    • extra 对象(可选) - 一个随崩溃日志发送的对象. 只有字符串属性能够被正确发送. 不支持发送嵌套对象,且属性名称和属性值必须小于64个字符长度.
    • crashesDirectoryString (可选)-用于临时存储 crashreports 的目录 (仅在崩溃报告器通过 process.crashReporter.start 启动时使用).

你需要调用任何其他的crashReporter API,您必须调用此方法. 在每个需要收集崩溃日志的进程 (主进程 / 渲染器进程) 中,也必须先调用此方法. 从不同的进程调用时, 可以传不同的配置给 crashReporter. start

注意 child_process 模块创建的子进程将无法访问 Electron 模块。 因此, 要收集它们的故障报告, 请用 process.crashReporter.start代替。 传递与上面相同的选项以及一个名为 crashesDirectory 的附加项, 它应指向一个目录, 以便临时存储崩溃报告。 你可以调用 process.crash() 使子进程崩溃,来测试结果。

注意: ,如果您在第一次调用 start 后需要发送 附加的/更新的 extra 参数, 在 macOS 上,你可以调用 addExtraParameter。而在 Linux 和 Windows 上,则使用 新的/更新的 extra 参数,再次调用 start即可。

Note: On macOS and windows, Electron uses a new crashpad client for crash collection and reporting. 如果要启用崩溃报告,则需要在主进程使用crashReporter.start初始化crashpad, 不管你想收集哪个进程的报告。 使用这种方式初始化后,crashpad将处理从所有进程收集的崩溃报告。 你仍然需要从渲染器进程或子进程中调用crashReporter.start ,否则崩溃报告将不包含companyName, productName和任何extra信息。

crashReporter.start(options)

  • options Object

    • companyName String
    • submitURL String - URL that crash reports will be sent to as POST.
    • productName String (optional) - Defaults to app.getName().
    • uploadToServer Boolean (optional) - Whether crash reports should be sent to the server Default is true.
    • ignoreSystemCrashHandler Boolean (optional) - Default is false.
    • extra Object (optional) - An object you can define that will be sent along with the report. Only string properties are sent correctly. Nested objects are not supported and the property names and values must be less than 64 characters long.
    • crashesDirectory String (optional) - Directory to store the crashreports temporarily (only used when the crash reporter is started via process.crashReporter.start).

You are required to call this method before using any other crashReporter APIs and in each process (main/renderer) from which you want to collect crash reports. You can pass different options to crashReporter.start when calling from different processes.

Note Child processes created via the child_process module will not have access to the Electron modules. Therefore, to collect crash reports from them, use process.crashReporter.start instead. Pass the same options as above along with an additional one called crashesDirectory that should point to a directory to store the crash reports temporarily. You can test this out by calling process.crash() to crash the child process.

Note: If you need send additional/updated extra parameters after your first call start you can call addExtraParameter on macOS or call start again with the new/updated extra parameters on Linux and Windows.

Note: On macOS and windows, Electron uses a new crashpad client for crash collection and reporting. If you want to enable crash reporting, initializing crashpad from the main process using crashReporter.start is required regardless of which process you want to collect crashes from. Once initialized this way, the crashpad handler collects crashes from all processes. You still have to call crashReporter.start from the renderer or child process, otherwise crashes from them will get reported without companyName, productName or any of the extra information.

crashReporter.getLastCrashReport()

返回 CrashReport:

返回上次崩溃报告的日期和ID。 只有已上传的崩溃报告将被返回;在磁盘上存在崩溃报告,除非上传,不然不会返回。 如果没有上传的报告,则返回null

crashReporter.getLastCrashReport()

Returns CrashReport:

Returns the date and ID of the last crash report. Only crash reports that have been uploaded will be returned; even if a crash report is present on disk it will not be returned until it is uploaded. In the case that there are no uploaded reports, null is returned.

crashReporter.getUploadedReports()

返回 CrashReport[]:

返回所有上传的崩溃报告。每个报告都包含日期和上传ID。

crashReporter.getUploadedReports()

Returns CrashReport[]:

Returns all uploaded crash reports. Each report contains the date and uploaded ID.

crashReporter.getUploadToServer()

返回 Boolean - 是否已将报告提交到服务器。通过start 方法或 setUploadToServer设置。

注意: 这个API仅可从主进程调用。

crashReporter.getUploadToServer()

Returns Boolean - Whether reports should be submitted to the server. Set through the start method or setUploadToServer.

Note: This API can only be called from the main process.

crashReporter.setUploadToServer(uploadToServer)

  • uploadToServer Boolean macOS - 是否将报告提交到服务器.

通常, 是否提交是由用户对系统进行偏好设置而决定的。不能在 start 之前调用该方法,否则无效.

注意: 这个API仅可从主进程调用。

crashReporter.setUploadToServer(uploadToServer)

  • uploadToServer Boolean macOS - Whether reports should be submitted to the server.

This would normally be controlled by user preferences. This has no effect if called before start is called.

Note: This API can only be called from the main process.

crashReporter.addExtraParameter(key, value) macOS Windows

  • key String - 参数键,长度必须小于64个字符
  • value String - 参数值, 长度必须小于64个字符

设置一个在发送崩溃报告时将额外包含的参数。 当调用 start 时, 除了通过 extra 选项设置的值之外, 此处指定值也将被发送。 This API is only available on macOS and windows, if you need to add/update extra parameters on Linux after your first call to start you can call start again with the updated extra options.

crashReporter.addExtraParameter(key, value) macOS Windows

  • key String - Parameter key, must be less than 64 characters long.
  • value String - Parameter value, must be less than 64 characters long.

Set an extra parameter to be sent with the crash report. The values specified here will be sent in addition to any values set via the extra option when start was called. This API is only available on macOS and windows, if you need to add/update extra parameters on Linux after your first call to start you can call start again with the updated extra options.

crashReporter.removeExtraParameter(key) macOS Windows

  • key String - 参数键,长度必须小于64个字符

从当前设定的参数中移除一个额外的参数, 以便它不会与崩溃报告一起发送。

crashReporter.removeExtraParameter(key) macOS Windows

  • key String - Parameter key, must be less than 64 characters long.

Remove a extra parameter from the current set of parameters so that it will not be sent with the crash report.

crashReporter.getParameters()

查看传递给崩溃报告的所有当前参数。

crashReporter.getParameters()

See all of the current parameters being passed to the crash reporter.

崩溃报告内容

崩溃报告将发送下面 multipart/form-data POST 型的数据给 submitURL:

  • ver String - Electron 的版本.
  • platform String - 例如 'win32'.
  • process_type String - 例如 'renderer'.
  • guid String - 例如 '5e1286fc-da97-479e-918b-6bfb0c3d1c72'.
  • _version String - package.json 里的版本号.
  • _productName String - crashReporter options 对象中的产品名字
  • prod String - 基础产品名字. 在这种情况下为 Electron.
  • _companyName String - crashReporter options 对象中的公司名称
  • upload_file_minidump File - minidump 格式的崩溃报告
  • All level one properties of the extra object in the crashReporter options object.

Crash Report Payload

The crash reporter will send the following data to the submitURL as a multipart/form-data POST:

  • ver String - The version of Electron.
  • platform String - e.g. 'win32'.
  • process_type String - e.g. 'renderer'.
  • guid String - e.g. '5e1286fc-da97-479e-918b-6bfb0c3d1c72'.
  • _version String - The version in package.json.
  • _productName String - The product name in the crashReporter options object.
  • prod String - Name of the underlying product. In this case Electron.
  • _companyName String - The company name in the crashReporter options object.
  • upload_file_minidump File - The crash report in the format of minidump.
  • All level one properties of the extra object in the crashReporter options object.