2016-05-02 81 views
1

我想POST將日誌記錄到Stackdriver的「自定義日誌」。這些功能是測試版,可能因此沒有說明,因此如何在App Engine上使用Logging with Java API。無論如何,我想說明我的問題:我使用這個API版本:Google App Engine。爲Stackdriver。使用Java記錄

"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; 

回答

1

我跑到非託管Python環境中的相同問題。我得到的東西工作,我可以看到你的代碼中至少有兩個問題。

  1. 日誌名稱需要遵循的模式: 「項目/ <項目編號> /日誌/ <登錄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」。

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