Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I see the whole HTTP request in Rails

I have a Rails application but after some time of development/debugging I realized that it would be very helpful to be able to see the whole HTTP request in the logfiles - log/development.log, not just the parameters.

I also want to have a separate logfile based on user, not session.

Any ideas will be appreciated!

like image 698
akafazov Avatar asked May 17 '10 15:05

akafazov


People also ask

How a request is processed in rails?

After the DNS gets resolved, the request hits a web server, which asks Rails what it has for that url . Rails goes to the routes. rb file first, which takes the URL and calls a corresponding controller action. The controller goes and gets whatever stuff it needs from the database using the relevant model .

How many requests can Ruby on Rails handle?

600 requests/second. 180 application instances (mongrel)


2 Answers

You can rapidly see the request.env in a view via:

  • VIEW: <%= request.env.inspect %>

If instead you want to log it in development log, from your controller:

  • CONTROLLER: Rails.logger.info(request.env)

Here you can see a reference for the Request object.

Rails automatically sets up logging to a file in the log/ directory using Logger from the Ruby Standard Library. The logfile will be named corresponding to your environment, e.g. log/development.log.

To log a message from either a controller or a model, access the Rails logger instance with the logger method:

class YourController < ActionController::Base
  def index
    logger.info request.env
  end
end

About the user, what are you using to authenticate It?

like image 61
microspino Avatar answered Oct 17 '22 17:10

microspino


That logger.info request.env code works fine in a Rails controller, but to see a more original version of that, or if you're using Grape or some other mounted app, you have to intercept the request on its way through the rack middleware chain...

Put this code in your lib directory (or at the bottom of application.rb):

require 'pp'
class Loggo
  def initialize(app)
    @app = app
  end
  def call(env)
    pp env
    @app.call(env)
  end
end

then in with the other configs in application.rb:

config.middleware.use "Loggo"
like image 5
AlexChaffee Avatar answered Oct 17 '22 15:10

AlexChaffee