Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save silviorelli/c391d9da5e02689f79ede1b0749d0542 to your computer and use it in GitHub Desktop.
Save silviorelli/c391d9da5e02689f79ede1b0749d0542 to your computer and use it in GitHub Desktop.
Logging headers, params, and body to console -- useful for debugging what a mobile app is sending Rails API backend server.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :global_request_logging
def global_request_logging
http_request_header_keys = request.headers.env.keys.select{|header_name| header_name.match("^HTTP.*|^X-User.*")}
http_request_headers = request.headers.env.select{|header_name, header_value| http_request_header_keys.index(header_name)}
puts '*' * 40
pp request.method
pp request.url
pp request.remote_ip
pp ActionController::HttpAuthentication::Token.token_and_options(request)
http_request_header_keys.each do |key|
puts ["%20s" % key.to_s, ':', request.headers[key].inspect].join(" ")
end
puts '-' * 40
params.keys.each do |key|
puts ["%20s" % key.to_s, ':', params[key].inspect].join(" ")
end
puts '*' * 40
begin
yield
ensure
puts response.body
end
end
end
Started GET "/play" for 192.168.1.170 at 2017-03-02 01:06:00 -0600
Cannot render console from 192.168.1.170! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PlayController#index as */*
****************************************
"GET"
"http://192.168.1.157:3000/play"
"192.168.1.170"
nil
HTTP_VERSION : "HTTP/1.1"
HTTP_HOST : "192.168.1.157:3000"
HTTP_ACCEPT_ENCODING : "gzip, deflate"
HTTP_COOKIE : "_matchpoint_session=UVVlZVRjNFBzdVdJUE9RNjhZQzVuSlo5SUJiUzdXSlpqWmtGbzJTV0RVZGVaMVpiaXJaQ0VuTXJBTUl4aTBiNXN2RUFHUDZwVzZxTFVENXZkbk1Ua245eGE0RFpSQ0dnbTM0WFovK09UM1E2WTRsaEJlRDByN2FlKzRHcnR6eElUSzVQS2pqenJCaVN5NHNQdGtlcS9nPT0tLTVyeWprNjl2OHB4eDdSYVBLU3FYVkE9PQ%3D%3D--4e6fccab19d7e3d3e13edd501dbfedd4f89de94d"
HTTP_CONNECTION : "keep-alive"
HTTP_IF_NONE_MATCH : "W/\"6a13f6bad95b09eda7bbe4a9a8d749a6\""
HTTP_ACCEPT : "*/*"
HTTP_USER_AGENT : "Jasonette/1.0 (iPhone; iOS 10.2.1; Scale/2.00)"
HTTP_ACCEPT_LANGUAGE : "en-US;q=1"
HTTP_X_USER_EMAIL : "m"
HTTP_X_USER_TOKEN : "QqMh2VAtlpWPAUh/9OYgs16/CfpoUzVKtU9vOYmr/pcPX+o7g8UdKdHk37B86UMxLbp4BoCT3KLuVrMaUjeHbA=="
----------------------------------------
controller : "play"
action : "index"
****************************************
Rendering play/index.jbuilder within layouts/jason
Rendered play/index.jbuilder within layouts/jason (5.0ms)
Rendered data/_authenticity_token.jbuilder (0.4ms)
Rendered data/_score.jbuilder (0.6ms)
Rendered layouts/_styles.jbuilder (1.1ms)
Rendered layouts/_header.jbuilder (0.3ms)
Rendered layouts/_footer.jbuilder (2.9ms)
Rendered layouts/_head.jbuilder (9.1ms)
{
"$jason": {
"head": {
"title": "Matchpoint",
"actions": {
"$foreground": {
"type": "$reload"
},
"$pull": {
"type": "$reload"
.
.
.
# The following gives you pretty formatted JSON
# add to your Gemfile: gem 'yajl-ruby'
# config/initializers/jbuilder.rb
require 'multi_json'
MultiJson.use :yajl
unless Rails.env.production?
MultiJson.dump_options = { pretty: true }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment