簡體   English   中英

谷歌應用引擎。 堆棧驅動程序。 使用 Java 進行日志記錄

[英]Google App Engine. Stackdriver. Logging with Java

我想將日志發布到 Stackdriver 的“自定義日志”。 這些功能是測試版,因此可能沒有描述,如何在 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();
}

獲得 Logging 客戶端后,我嘗試將條目推送到日志:

    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”。

我希望這將有助於解決您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM