From c83d9a11c00bc13e1f8f0fa0e8fb6185cacd5fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 19 Oct 2011 22:39:11 +0200 Subject: Unify logger and taggedlogging middleware as both address logging concerns. --- railties/lib/rails/rack/logger.rb | 38 +++++++++++++++++++++---------- railties/lib/rails/rack/tagged_logging.rb | 36 ----------------------------- 2 files changed, 26 insertions(+), 48 deletions(-) delete mode 100644 railties/lib/rails/rack/tagged_logging.rb (limited to 'railties/lib/rails/rack') 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 -- cgit v1.2.3