aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-10-19 22:39:11 +0200
committerJosé Valim <jose.valim@gmail.com>2011-10-19 22:39:11 +0200
commitc83d9a11c00bc13e1f8f0fa0e8fb6185cacd5fc9 (patch)
tree0f551c45ee8ecfd954532217dabe437c768ee972
parent6c126015a676c376f1646713fbb739049a783238 (diff)
downloadrails-c83d9a11c00bc13e1f8f0fa0e8fb6185cacd5fc9.tar.gz
rails-c83d9a11c00bc13e1f8f0fa0e8fb6185cacd5fc9.tar.bz2
rails-c83d9a11c00bc13e1f8f0fa0e8fb6185cacd5fc9.zip
Unify logger and taggedlogging middleware as both address logging concerns.
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails/application.rb3
-rw-r--r--railties/lib/rails/rack.rb1
-rw-r--r--railties/lib/rails/rack/logger.rb38
-rw-r--r--railties/lib/rails/rack/tagged_logging.rb36
5 files changed, 28 insertions, 52 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 181019f851..7f7b24804d 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,6 +1,6 @@
*Rails 3.2.0 (unreleased)*
-* Added Rails::Rack::TaggedLogging middleware by default that will apply any tags set in config.log_tags to the newly ActiveSupport::TaggedLogging Rails.logger. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications [DHH]
+* Updated Rails::Rack::Logger middleware to apply any tags set in config.log_tags to the newly ActiveSupport::TaggedLogging Rails.logger. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications [DHH]
* Default options to `rails new` can be set in ~/.railsrc [Guillermo Iguaran]
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index a097cfd1be..82fffe86bb 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -165,8 +165,7 @@ module Rails
middleware.use ::Rack::Runtime
middleware.use ::Rack::MethodOverride
middleware.use ::ActionDispatch::RequestId
- middleware.use ::Rails::Rack::TaggedLogging, config.log_tags
- middleware.use ::Rails::Rack::Logger # must come after Rack::MethodOverride to properly log overridden methods
+ middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
middleware.use ::ActionDispatch::ShowExceptions, config.consider_all_requests_local
middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
if config.action_dispatch.x_sendfile_header.present?
diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb
index b78293e570..d1ee96f7fd 100644
--- a/railties/lib/rails/rack.rb
+++ b/railties/lib/rails/rack.rb
@@ -3,6 +3,5 @@ module Rails
autoload :Debugger, "rails/rack/debugger"
autoload :Logger, "rails/rack/logger"
autoload :LogTailer, "rails/rack/log_tailer"
- autoload :TaggedLogging, "rails/rack/tagged_logging"
end
end
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
index 4d388c4d10..89de10c83d 100644
--- a/railties/lib/rails/rack/logger.rb
+++ b/railties/lib/rails/rack/logger.rb
@@ -1,32 +1,46 @@
require 'active_support/core_ext/time/conversions'
+require 'active_support/core_ext/object/blank'
module Rails
module Rack
# Log the request started and flush all loggers after it.
class Logger < ActiveSupport::LogSubscriber
- def initialize(app)
- @app = app
+ def initialize(app, tags=nil)
+ @app, @tags = app, tags.presence
end
def call(env)
- before_dispatch(env)
- @app.call(env)
- ensure
- after_dispatch(env)
+ if @tags
+ Rails.logger.tagged(compute_tags(env)) { call_app(env) }
+ else
+ call_app(env)
+ end
end
protected
- def before_dispatch(env)
+ def call_app(env)
request = ActionDispatch::Request.new(env)
path = request.filtered_path
-
- info "\n\n"
- info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
+ Rails.logger.info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
+ @app.call(env)
+ ensure
+ ActiveSupport::LogSubscriber.flush_all!
end
- def after_dispatch(env)
- ActiveSupport::LogSubscriber.flush_all!
+ def compute_tags(env)
+ request = ActionDispatch::Request.new(env)
+
+ @tags.collect do |tag|
+ case tag
+ when Proc
+ tag.call(request)
+ when Symbol
+ request.send(tag)
+ else
+ tag
+ end
+ end
end
end
end
diff --git a/railties/lib/rails/rack/tagged_logging.rb b/railties/lib/rails/rack/tagged_logging.rb
deleted file mode 100644
index c519d7c3e6..0000000000
--- a/railties/lib/rails/rack/tagged_logging.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-module Rails
- module Rack
- # Enables easy tagging of any logging activity that occurs within the Rails request cycle. The tags are configured via the
- # config.log_tags setting. The tags can either be strings, procs taking a request argument, or symbols representing method
- # names on request (so :uuid will result in request.uuid being added as a tag).
- class TaggedLogging
- def initialize(app, tags = nil)
- @app, @tags = app, tags
- end
-
- def call(env)
- if @tags
- Rails.logger.tagged(compute_tags(env)) { @app.call(env) }
- else
- @app.call(env)
- end
- end
-
- private
- def compute_tags(env)
- request = ActionDispatch::Request.new(env)
-
- @tags.collect do |tag|
- case tag
- when Proc
- tag.call(request)
- when Symbol
- request.send(tag)
- else
- tag
- end
- end
- end
- end
- end
-end