読者です 読者をやめる 読者になる 読者になる

Android LogCat に HTTP の通信ログを出力する方法

以下のコードを適当に埋め込む。

java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");

LogCat したい PC で以下のコマンドを実行する。

adb shell setprop log.tag.org.apache.http VERBOSE
adb shell setprop log.tag.org.apache.http.wire VERBOSE
adb shell setprop log.tag.org.apache.http.headers VERBOSE

なんかこんなログが出るようになる。便利。

>> "GET /api HTTP/1.1[EOL]"
>> "Host: api.example.com[EOL]"
>> "Connection: Keep-Alive[EOL]"
>> "User-Agent: androidapp.testapp/1.0.0 (Linux; U; Android 4.2.2; ja-JP; SC-04E Build/JDQ39)[EOL]"
>> "[EOL]"
>> GET /api HTTP/1.1
>> Host: api.example.com
>> Connection: Keep-Alive
>> User-Agent: androidapp.testapp/1.0.0 (Linux; U; Android 4.2.2; ja-JP; SC-04E Build/JDQ39)
<< "HTTP/1.1 200 OK[EOL]"
<< "Server: nginx[EOL]"
<< "Date: Wed, 18 Sep 2013 06:56:11 GMT[EOL]"
<< "Content-Type: application/json; charset=utf-8[EOL]"
<< "Content-Length: 3766[EOL]"
<< "Connection: keep-alive[EOL]"
<< "Keep-Alive: timeout=3[EOL]"
<< "Cache-Control: private[EOL]"
<< "X-Content-Type-Options: nosniff[EOL]"
<< "X-Frame-Options: DENY[EOL]"
<< HTTP/1.1 200 OK
<< Server: nginx
<< Date: Wed, 18 Sep 2013 06:56:11 GMT
<< Content-Type: application/json; charset=utf-8
<< Content-Length: 15
<< Connection: keep-alive
<< Keep-Alive: timeout=3
<< Cache-Control: private
<< X-Content-Type-Options: nosniff
<< X-Frame-Options: DENY
<< "{"api":"test"}"