日志记录

在开发任何类型的应用时,您常常需要记录信息 诊断开发过程中的故障,发现和诊断客户问题, 以及用于其他用途

Apps 脚本提供三种不同的日志记录机制:

  • 内置的 Apps 脚本执行日志。 此日志是轻量级的,并且会实时流式传输,但只会保留 。

  • 使用 Cloud Logging 中的 Cloud Logging 界面, 开发者控制台(提供的日志会在用户首次访问某个地区后 创建过程。

  • Error Reporting 界面 ,该平台会收集并记录发生的错误 。

下文将对这些组件进行介绍。除了上述机制外 您还可以构建自己的日志记录器代码,例如 记录到日志电子表格中。 或 JDBC 数据库

使用 Apps 脚本执行日志

登录 Apps 脚本的一个基本方法是使用内置的 执行日志。如需查看这些日志,请在顶部 在编辑器中,点击执行日志。运行函数或使用 调试程序,实时流式传输日志。

您可以使用 Loggerconsole 中的日志记录服务 。

这些日志用于在开发和调试期间进行简单检查, 而且不会持续很长时间

例如,请考虑以下函数:

utils/logging.gs
/**
 * Logs Google Sheet information.
 * @param {number} rowNumber The spreadsheet row number.
 * @param {string} email The email to send with the row data.
 */
function emailDataRow(rowNumber, email) {
  console.log('Emailing data row ' + rowNumber + ' to ' + email);
  try {
    const sheet = SpreadsheetApp.getActiveSheet();
    const data = sheet.getDataRange().getValues();
    const rowData = data[rowNumber - 1].join(' ');
    console.log('Row ' + rowNumber + ' data: ' + rowData);
    MailApp.sendEmail(email, 'Data in row ' + rowNumber, rowData);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
}

当使用输入“2”运行此脚本时和“john@example.com”该 将写入以下日志:

[16-09-12 13:50:42:193 PDT] 将数据第 2 行通过电子邮件发送给 john@example.com
[16-09-12 13:50:42:271 PDT] 第 2 行数据:费用 103.24

Cloud Logging

Apps 脚本还提供对 Google Cloud Platform (GCP) 的部分访问权限 Cloud Logging 服务。当您 需要持续数天的日志记录,或者需要更复杂的日志记录 解决方案,Cloud Logging 是 选择。请参阅 Cloud Logging 配额和限制 了解数据保留和其他配额详情。

如果您需要更多日志记录配额,可以 提交 Google Cloud Platform 配额申请。 您必须有权访问 Cloud Platform 项目 脚本所用的资源。

使用 Cloud Logging

Cloud 日志已附加到 Google Cloud 项目 与 Apps 脚本相关联。你可以 Apps 脚本信息中心内查看日志。

要充分利用 Cloud Logging 及其功能,请使用 标准 Google Cloud 项目 与脚本项目相关联这样,您就可以直接在 GCP 控制台 并提供更多查看和过滤选项

记录时,在隐私保护方面应尽量避免记录任何个人数据 用户的相关信息,例如电子邮件地址。Cloud 日志 自动标记为 有效的用户密钥 可以在必要时使用查找特定用户的日志消息。

您可以使用 Apps 脚本提供的函数 console 服务。

以下示例展示了如何使用 console 服务来记录 Cloud Operations 中的信息。

utils/logging.gs
/**
 * Logs the time taken to execute 'myFunction'.
 */
function measuringExecutionTime() {
  // A simple INFO log message, using sprintf() formatting.
  console.info('Timing the %s function (%d arguments)', 'myFunction', 1);

  // Log a JSON object at a DEBUG level. The log is labeled
  // with the message string in the log viewer, and the JSON content
  // is displayed in the expanded log structure under "jsonPayload".
  const parameters = {
    isValid: true,
    content: 'some string',
    timestamp: new Date()
  };
  console.log({message: 'Function Input', initialData: parameters});
  const label = 'myFunction() time'; // Labels the timing log entry.
  console.time(label); // Starts the timer.
  try {
    myFunction(parameters); // Function to time.
  } catch (e) {
    // Logs an ERROR message.
    console.error('myFunction() yielded an error: ' + e);
  }
  console.timeEnd(label); // Stops the timer, logs execution duration.
}

有效的用户密钥

临时有效的用户密钥提供了一种便利的方式来找出 Cloud 日志条目,而不泄露这些用户的身份。键 都按脚本定义,大约每月更改一次以增强安全性 用户是否应向开发者透露其身份,例如在举报时 一个问题。

临时有效的用户密钥优于电子邮件等日志记录标识符 地址,因为:

  • 您不必向日志记录添加任何内容;他们已经在那里了!
  • 它们不需要用户授权。
  • 它们可以保护用户隐私。

如需在 Cloud 日志条目中查找临时有效的用户密钥,请按以下步骤操作: 在 Google Cloud 控制台中查看您的 Cloud 日志。 只有在脚本项目使用 标准 Google Cloud 项目 其他项目。在控制台中打开 Google Cloud 项目后 选择感兴趣的日志条目并展开即可查看 元数据 >标签 >script.googleapis.com/user_key.

您还可以通过调用 Session.getTemporaryActiveUserKey() 。使用此方法的一种方法是向用户显示密钥 会非常有用然后,用户可以选择包含自己的密钥 ,以帮助您确定相关日志。

异常日志记录

异常日志记录功能会发送脚本项目代码中未处理的异常 以及堆栈轨迹

如需查看异常日志,请按以下步骤操作:

  1. 打开 Apps 脚本项目。
  2. 点击左侧的执行作业
  3. 点击顶部的添加过滤条件 >状态
  4. 选中失败超时复选框。

您还可以在 GCP 控制台中查看记录的异常 如果您的脚本项目使用的是 标准 Google Cloud 项目 其他项目。

启用异常日志记录

默认情况下,系统会为新项目启用异常日志记录功能。启用例外情况 日志记录,请按以下步骤操作:

  1. 打开脚本项目。
  2. 点击左侧的项目设置
  3. 选中将未捕获的异常记录到 Cloud Operations 中复选框。

Error Reporting

异常日志记录功能会自动与 Cloud Error Reporting, 这项服务可汇总并显示脚本中产生的错误。 您可以 在 Google Cloud 控制台中查看您的 Cloud 错误报告。 如果系统提示您“设置 Error Reporting”这是因为 您的脚本尚未记录任何异常。除此以外,您无需进行任何设置 启用异常日志记录

日志记录要求

使用内置执行日志没有任何要求。

您可以在 Apps 脚本信息中心。但是, 充分利用 Cloud Logging 和错误报告 添加到该脚本的 GCP 项目只有在您的脚本项目 使用标准 Google Cloud 项目