2016-05-02 16 views
1

Stackdriverの "カスタムログ"にログをポストします。これらの機能はベータ版であるため、App Engine上でJava APIを使用してロギングを使用する方法については説明がありません。とにかく私は私の問題を説明したい:私は取得した後Google App Engineスタックドライバー。

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(); 
} 

:私はこのようなログオブジェクトを(私はこれが正しいと願っています)の構築まず、だから、

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

:私は、このAPIのバージョンを使用します

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に感謝します。私が走った

private Map<String, Object> entriesMap; 

答えて

1

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()); 
    } 

このコードは

これによりEntriesMapがあるロギングAPIの最後のバージョンで正常に動作します:ここでは完全なコードは、ログにデータを送信する方法であり、管理されていないPython環境でも同じ問題になります。私は物事が働いていると私はあなたのコードで少なくとも2つの問題を見ることができます。

  1. ログ名はパターンに従う必要があります: "プロジェクト/ <プロジェクト-ID > /ログ/ <ログ-ID >を"。 https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry#SCHEMA_REPRESENTATION

  2. リソース記述子をログエントリ(lEntry)と書き込みログエントリ要求(writeLogEntriesRequest)の両方に追加する必要があります。 GAEの場合、リソースタイプフィールドを "gae_app"に設定し、GAE展開を識別する3つのラベル、 "project_id"、 "module_id"、および "version_id"をリソースに追加する必要があります。

私はあなたの問題の解決に役立つことを願っています!