aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2012-09-26 15:08:25 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2012-09-27 10:46:50 -0700
commit35a17502b6054ab6d6407c0af902a441e8ae8215 (patch)
tree0e02ee478a91c2dfd5e99ef82ec8898b2528349b /railties
parent20f5f08d3b4df0f31099378c8178a9a78db52419 (diff)
downloadrails-35a17502b6054ab6d6407c0af902a441e8ae8215.tar.gz
rails-35a17502b6054ab6d6407c0af902a441e8ae8215.tar.bz2
rails-35a17502b6054ab6d6407c0af902a441e8ae8215.zip
Tune up Rails::Rack::Logger. Only put space between requests in development logs.
Conflicts: railties/test/application/rack/logger_test.rb
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/rack/logger.rb37
-rw-r--r--railties/test/application/rack/logger_test.rb12
2 files changed, 29 insertions, 20 deletions
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
index 798fb32677..d0d053e7ed 100644
--- a/railties/lib/rails/rack/logger.rb
+++ b/railties/lib/rails/rack/logger.rb
@@ -3,36 +3,45 @@ require 'active_support/core_ext/object/blank'
module Rails
module Rack
- # Log the request started and flush all loggers after it.
+ # Sets log tags, logs the request, calls the app, and flushes the logs.
class Logger < ActiveSupport::LogSubscriber
- def initialize(app, tags=nil)
- @app, @tags = app, tags.presence
+ def initialize(app, taggers = nil)
+ @app, @taggers = app, taggers || []
end
def call(env)
- if @tags && Rails.logger.respond_to?(:tagged)
- Rails.logger.tagged(compute_tags(env)) { call_app(env) }
+ request = ActionDispatch::Request.new(env)
+
+ # Put some space between requests in development logs.
+ Rails.logger.info "\n\n" if Rails.env.development?
+
+ if Rails.logger.respond_to?(:tagged)
+ Rails.logger.tagged(compute_tags(request)) { call_app(request, env) }
else
- call_app(env)
+ call_app(request, env)
end
end
protected
- def call_app(env)
- request = ActionDispatch::Request.new(env)
- path = request.filtered_path
- Rails.logger.info "\n\n"
- Rails.logger.info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
+ def call_app(request, env)
+ Rails.logger.info started_request_message(request)
@app.call(env)
ensure
ActiveSupport::LogSubscriber.flush_all!
end
- def compute_tags(env)
- request = ActionDispatch::Request.new(env)
+ # Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700
+ def started_request_message(request)
+ 'Started %s "%s" for %s at %s' % [
+ request.request_method,
+ request.filtered_path,
+ request.ip,
+ Time.now.to_default_s ]
+ end
- @tags.collect do |tag|
+ def compute_tags(request)
+ @taggers.collect do |tag|
case tag
when Proc
tag.call(request)
diff --git a/railties/test/application/rack/logger_test.rb b/railties/test/application/rack/logger_test.rb
index 387eb25525..1e134083db 100644
--- a/railties/test/application/rack/logger_test.rb
+++ b/railties/test/application/rack/logger_test.rb
@@ -21,25 +21,25 @@ module ApplicationTests
end
def logs
- @logs ||= @logger.logged(:info)
+ @logs ||= @logger.logged(:info).join("\n")
end
test "logger logs proper HTTP verb and path" do
get "/blah"
wait
- assert_match(/^Started GET "\/blah"/, logs[0])
+ assert_match 'Started GET "/blah"', logs
end
test "logger logs HTTP verb override" do
- post "/", {:_method => 'put'}
+ post "/", _method: 'put'
wait
- assert_match(/^Started PUT "\/"/, logs[0])
+ assert_match 'Started PUT "/"', logs
end
test "logger logs HEAD requests" do
- post "/", {:_method => 'head'}
+ post "/", _method: 'head'
wait
- assert_match(/^Started HEAD "\/"/, logs[0])
+ assert_match 'Started HEAD "/"', logs
end
end
end