set_debug_outputでnet/httpの通信内容を確認できる。
これを用いてgemの中でもnet/httpを使っていれば通信内容を確認できる方法をメモする。
スクリプトで確認する方法
- スクリプトで確認する際は下記のようなコードを先頭に配置する。
require 'net/http'
class Net::HTTP
alias :create :initialize
def initialize(*args)
create(*args)
self.set_debug_output $stderr
$stderr.sync = true
end
end
railsで確認する方法
-
passengerにおいてapacheのerror_logに出力
-
上記のコードをconfig/initializers/http.rbあたりに配置
-
railsログに出力
-
以下のようなコードを配置
require 'net/http'
class Net::HTTP
alias :create :initialize
def initialize(*args)
logger = Rails.logger
def logger.<< log
info "HTTP_LOG:#{log}"
end
create(*args)
self.set_debug_output logger
end
end
出力内容
以下のような感じで通信のデバッグログが出力される
opening connection to www.google.co.jp...
opened
<- "GET / HTTP/1.1\r\nConnection: close\r\nHost: www.google.co.jp\r\nAccept: */*\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "Date: Sun, 30 Sep 2012 01:49:21 GMT\r\n"
-> "Expires: -1\r\n"
-> "Cache-Control: private, max-age=0\r\n"
-> "Content-Type: text/html; charset=Shift_JIS\r\n"
-> "Set-Cookie: PREF=ID=be9181a7dc77ebf4:FF=0:TM=1348969761:LM=1348969761:S=bvbJqDikB9M8WLUZ; expires=Tue, 30-Sep-2014 01:49:21 GMT; path=/; domain=.google.co.jp\r\n"
-> "Set-Cookie: NID=64=kBPaI8UJL-M-Q_EBwtwutN_hVKOV2JYL6w-2lNpNGya83gDk2bLmUu6IkvrSE3lbDbVCpxcDciGHcdcd0TxlOyIrT34AOJdo_gUALHHJ6aVu0O75QscfZkchFxPWO-3W; expires=Mon, 01-Apr-2013 01:49:21 GMT; path=/; domain=.google.co.jp; HttpOnly\r\n"
-> "P3P: CP=\"This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.\"\r\n"
-> "Server: gws\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading all...
-> "[GETしたbody]"
read 43792 bytes
Conn close