【发布时间】:2021-02-07 23:01:59
【问题描述】:
我正在将 Stackdriver Error Logging REST API 与 Apps 脚本一起使用。
文档位于:
https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/list
查询参数字符串中需要组 ID。 我需要组 ID,但我不知道从哪里获取组名或组 ID。我查看了 IAM 设置:
https://console.cloud.google.com/iam-admin/settings?authuser=0
IAM 设置有一个组部分,但我没有任何组。 我不知道从哪里获得组 ID。 在哪里可以找到组 ID?
我得到的错误是:
{
"error": {
"code": 400,
"message": "Missing group_id.",
"status": "INVALID_ARGUMENT"
}
}
我使用的代码是:
function getStackdriverLogs(){
var entries,groupId,httpResponse,logs,options,param,projectId,url;
/*
Get StackDriver Logs -
*/
/*
The Stackdriver Error Reporting API (Error Reporting API) must be enabled -
https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/list
*/
groupId = "Put group ID here";
projectId = "Put the Cloud Project ID here";
param = 'projects/' + projectId;
url = "https://clouderrorreporting.googleapis.com/v1beta1/" + param + "/events";
url = url + "?groupId=" + groupId;
options = {};
options.muteHttpExceptions = true;
options.headers = {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()};
options.contentType = "application/json";
options.method = 'get';
httpResponse = UrlFetchApp.fetch(url,options);
Logger.log('httpResponse.getResponseCode(): ' + httpResponse.getResponseCode());
if (httpResponse.getResponseCode() !== 200) {
return httpResponse.getContentText();
}
logs = httpResponse.getContentText();
Logger.log('logs: ' + logs)
logs = JSON.parse(logs);
entries = logs.entries;
Logger.log('pgTkn',pgTkn)
}
【问题讨论】:
-
错误被分组,每个组都有一个id。组的 id 可以在 url 中找到。
-
我查看了两个用于获取 Apps 脚本日志的不同 API,即 Cloud Error Reporting API 和 Cloud Logging API。它们不一样。 Cloud Logging API 可以过滤 Apps 脚本函数的日志。我在 Cloud Log 中拥有的唯一日志来自我的 Apps 脚本项目,但可能有来自多个不同来源的日志。您的 Cloud Logging 中可能有来自多种不同编程语言的条目。要使用过滤器
app_script_function,请参阅:https://stackoverflow.com/a/64530003/2946873
标签: google-apps-script error-logging google-cloud-stackdriver