Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full URLs in Rails logs

Is it possible to get the full URLs in the logs of a Rails application? Currently I'm getting something like:

Started GET "/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200

but I need to get:

Started GET "http://localhost:3000/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200

because this is an app where the domains are important.

like image 638
pupeno Avatar asked Dec 09 '22 06:12

pupeno


1 Answers

The line is coming from the middleware Rails::Rack::Logger, which is in railties. At the simplest, you could just override the method doing the logging, e.g., in an initializer:

class Rails::Rack::Logger < ActiveSupport::LogSubscriber
  protected

  def before_dispatch(env)
    request = ActionDispatch::Request.new(env)
    info "\n\nStarted #{request.request_method} \"#{request.url}\" for #{request.ip} at #{Time.now.to_default_s}"
  end
end

If you'd rather not override the logger, you could always add your own, which you've already created, then delete Rails::Rack::Logger from the stack via:

config.middleware.insert_before(Rails::Rack::Logger, YourLogger)
config.middleware.delete(Rails::Rack::Logger)

If you go this route you might check out the rack logger for its usage of ActionDispatch::Request, and be sure to do the other job it does of flushing the log subscriber cache after dispatch, via ActiveSupport::LogSubscriber.flush_all!

like image 130
numbers1311407 Avatar answered Dec 25 '22 21:12

numbers1311407