I find myself needing to have the full URLs in Rails’ logs. Normally you get something like:
[sourcecode lang=”text”]
Started GET "/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200
[/sourcecode]
but I needed
[sourcecode lang=”text”]
Started GET "http://foo.bar:3000/" for 127.0.0.1 at 2011-08-27 13:13:10 +0200
[/sourcecode]
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:
[sourcecode lang=”ruby”]
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
[/sourcecode]
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