ねぎ嫌い

始業前に学んだことを小出しに。最近はHacker Newsの人気記事をまとめてみたり。

Google API をJava経由で叩く時にログを出す

com.google.api.client.http.HttpRequest内でログレベルがLEVEL.CONFIGだったらログを出すとしている。

  public HttpResponse execute() throws IOException {
      //省略
      Logger logger = HttpTransport.LOGGER;
      boolean loggable = loggingEnabled && logger.isLoggable(Level.CONFIG);
      //省略
      if (loggable) {
            logger.config(logbuf.toString());
      }     
      //省略
  }

結論としては、動かしているJREのlib以下にあるlogging.propertiesを編集すれば良い。

handlers= java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level = CONFIG
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

こうなっていれば出るはず。

最終的に以下のような出力が得られる。
ここではGSuiteのAdmin SDKを利用した際のログを示す。

2017/10/12 17:42:04 com.google.api.client.http.HttpRequest execute
設定: -------------- REQUEST  --------------
GET https://www.googleapis.com/admin/directory/v1/groups/${group_mail}/members?maxResults=200
Accept-Encoding: gzip
Authorization: Bearer ${token}
User-Agent: GoogleApps-Test Google-API-Java-Client Google-HTTP-Java-Client/1.20.0 (gzip)

2017/10/12 17:42:04 com.google.api.client.http.HttpRequest execute
設定: curl -v --compressed -H 'Accept-Encoding: gzip' -H 'Authorization: Bearer ${token}' -H 'User-Agent: GoogleApps-Test Google-API-Java-Client Google-HTTP-Java-Client/1.20.0 (gzip)' -- 'https://www.googleapis.com/admin/directory/v1/groups/${group_mail}/members?maxResults=200'
2017/10/12 17:42:05 com.google.api.client.http.HttpResponse <init>
設定: -------------- RESPONSE --------------
HTTP/1.1 200 OK
ETag: "${etag}"
X-XSS-Protection: 1; mode=block
Expires: Thu, 12 Oct 2017 08:42:07 GMT
Server: GSE
X-Content-Type-Options: nosniff
Cache-Control: private, max-age=0, must-revalidate, no-transform
X-Frame-Options: SAMEORIGIN
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,35"
Transfer-Encoding: chunked
Vary: X-Origin
Vary: Origin
Date: Thu, 12 Oct 2017 08:42:07 GMT
Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8

2017/10/12 17:42:05 com.google.api.client.util.LoggingByteArrayOutputStream close
設定: Total: 111 bytes
2017/10/12 17:42:05 com.google.api.client.util.LoggingByteArrayOutputStream close
設定: {
 "kind": "admin#directory#members",
 "etag": "\"${etag}\""
}