我需要記錄所有的請求,包括HTTP標頭,身體等到一個特定的網址。我試過這段代碼:如何記錄某個url的整個請求(標題,正文等)?
def index
global_request_logging
end
private
def global_request_logging
http_request_header_keys = request.headers.keys.select{|header_name| header_name.match("^HTTP.*")}
http_request_headers = request.headers.select{|header_name, header_value| http_request_header_keys.index(header_name)}
logger.info "Received #{request.method.inspect} to #{request.url.inspect} from #{request.remote_ip.inspect}. Processing with headers #{http_request_headers.inspect} and params #{params.inspect}"
begin
yield
ensure
logger.info "Responding with #{response.status.inspect} => #{response.body.inspect}"
end
end
但它表示request.headers
不包含名爲keys
方法。此外,我認爲應該有一個更簡單的方法或標準來做到這一點。最好不要使用寶石。
標題註釋沒有鍵方法。 http://api.rubyonrails.org/classes/ActionDispatch/Http/Headers.html 有一個名爲'env'的實例變量,它是一個包含值的散列。所以你應該可以做'request.headers.env.keys'。要理解標題類的工作方式,請查看源代碼,它非常小:https://github.com/rails/rails/blob/e7b0947afdc8c9f4119d06628070667b9e7875d1/actionpack/lib/action_dispatch/http/headers.rb – Brennan 2015-02-24 03:44:06