Full URL in Rails’ logs

I find myself needing to have the full URLs in Rails’ logs. Normally you get something like:

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

but I needed

Started GET "http://foo.bar:3000/" for at 2011-08-27 13:13:10 +0200

because the app does different things depending on the domain and when it fails, I have to know which URL was hit. The solution I ended up with was adding this in an initializer:

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

  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}"

That’s monkey-patching Rails’ own logger. Credit for the solution goes to numbers1311407.

My question for the people using Rails, do you think having a configurable logger in Rails would be useful or nice? If so, I could make a patch for Rails but I have made patches before that failed to gather the needed popularity and thus were ignored. I’m not wasting my time like that again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.