Google 应用引擎。堆栈驱动程序。使用 Java 记录

Google App Engine. Stackdriver. Logging with Java

我想 POST 登录到 Stackdriver 的 "Custom Logs"。这些功能是测试版,因此可能没有描述如何在 App Engine 上使用 Java API 进行日志记录。无论如何,我想描述我的问题:我使用这个 API 版本:

"com.google.apis:google-api-services-logging:v2beta1-rev10-1.21.0"

所以,首先我像这样构建日志对象(我希望这是正确的):

public static Logging createAuthorizedClient() throws IOException {
    // Create the credential
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    if (credential.createScopedRequired()) {
        credential = credential.createScoped(LoggingScopes.all());
    }

    return new Logging.Builder(transport, jsonFactory, credential).setApplicationName(SharedConstants.APPLICATION_ID).build();
}

获取日志客户端后,我尝试将条目推送到日志:

    LogEntry lEntry = new LogEntry();
    lEntry.setTextPayload("I want to see this log!");

    WriteLogEntriesRequest writeLogEntriesRequest = new WriteLogEntriesRequest();

    writeLogEntriesRequest.setLogName("My Super log");
    List<LogEntry> listEntries = new ArrayList<>();
    listEntries.add(lEntry);

    writeLogEntriesRequest.setEntries(listEntries);

    Logging logging = LoggingManager.createAuthorizedClient();
    Write write = logging.entries().write(writeLogEntriesRequest);
    WriteLogEntriesResponse writeLogResponse = write.execute();

但我得到的是:

  com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 OK
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid resource id",
    "reason" : "badRequest"
  } ],
  "message" : "Invalid resource id",
  "status" : "INVALID_ARGUMENT"
}

===更新:工作解决方案===

感谢 mshamma。这是完整的代码,如何将数据发送到日志记录:

    public boolean send() {
        WriteLogEntriesResponse response = null;
        try {
            final String now = getNowUtc();
            final String insertId = "entry-at-" + now;
            final Map<String, String> labels = ImmutableMap.of("project_id", SharedConstants.APPLICATION_ID, "name",
                    "projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName);

            Logging service = createAuthorizedClient();
            MonitoredResource ressource = new MonitoredResource();
            ressource.setType("logging_log");
            ressource.setLabels(labels);

            LogEntry entry = new LogEntry().setInsertId(insertId).setResource(ressource).setTimestamp(now)
                    .setJsonPayload(this.entriesMap)
                    .setLogName("projects/" + SharedConstants.APPLICATION_ID + "/logs/" + this.logName)
                    .setSeverity(this.severity);
            WriteLogEntriesRequest content = (new WriteLogEntriesRequest())
                    .setEntries(Collections.singletonList(entry));
            response = service.entries().write(content).execute();
        } catch (Exception e) {
        }
        return response != null;
    }

    private static String getNowUtc() {
        SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormatUtc.format(new Date());
    }

此代码适用于最新版本的日志记录 api

因此 EntriesMap 是:

private Map<String, Object> entriesMap;

我 运行 在非托管 Python 环境中遇到了同样的问题。我让一切正常,我可以在您的代码中看到至少两个问题。

  1. 日志名称需要遵循以下模式:"projects/<project-id>/logs/<log-id>"。请在此处查看该字段的文档:https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry#SCHEMA_REPRESENTATION

  2. 您应该向日志条目 (lEntry) 和写入日志条目请求 (writeLogEntriesRequest) 添加资源描述符。对于 GAE,资源类型字段应设置为 "gae_app",并且您必须向资源添加三个标签以标识您的 GAE 部署:"project_id"、"module_id" 和 "version_id".

希望能帮助您解决问题![​​=11=]