aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2011-10-19 12:59:33 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2011-10-19 12:59:33 -0500
commitafde6fdd5ef3e6b0693a7e330777e85ef4cffddb (patch)
treee8cc45b642d1908a1768117eb343ee3bf99c5596 /railties/lib/rails/rack
parent3a746f7c48936bac1c08dcf229c7c8fc74fdfc13 (diff)
downloadrails-afde6fdd5ef3e6b0693a7e330777e85ef4cffddb.tar.gz
rails-afde6fdd5ef3e6b0693a7e330777e85ef4cffddb.tar.bz2
rails-afde6fdd5ef3e6b0693a7e330777e85ef4cffddb.zip
Added X-Request-Id tracking and TaggedLogging to easily log that and other production concerns
Diffstat (limited to 'railties/lib/rails/rack')
-rw-r--r--railties/lib/rails/rack/logger.rb4
-rw-r--r--railties/lib/rails/rack/tagged_logging.rb39
2 files changed, 41 insertions, 2 deletions
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
index 3be262de08..4d388c4d10 100644
--- a/railties/lib/rails/rack/logger.rb
+++ b/railties/lib/rails/rack/logger.rb
@@ -21,8 +21,8 @@ module Rails
request = ActionDispatch::Request.new(env)
path = request.filtered_path
- info "\n\nStarted #{request.request_method} \"#{path}\" " \
- "for #{request.ip} at #{Time.now.to_default_s}"
+ info "\n\n"
+ info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
end
def after_dispatch(env)
diff --git a/railties/lib/rails/rack/tagged_logging.rb b/railties/lib/rails/rack/tagged_logging.rb
new file mode 100644
index 0000000000..7980319b37
--- /dev/null
+++ b/railties/lib/rails/rack/tagged_logging.rb
@@ -0,0 +1,39 @@
+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 the symbols :uuid or :subdomain.
+ # The latter two are then automatically expanded to request.uuid and request.subdaomins.first -- the two most common tags
+ # desired in production logs.
+ 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 :uuid
+ request.uuid
+ when :subdomain
+ request.subdomains.first
+ else
+ tag
+ end
+ end
+ end
+ end
+ end
+end