aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/rack
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/rack')
-rw-r--r--railties/lib/rails/rack/debugger.rb24
-rw-r--r--railties/lib/rails/rack/log_tailer.rb38
-rw-r--r--railties/lib/rails/rack/logger.rb86
3 files changed, 148 insertions, 0 deletions
diff --git a/railties/lib/rails/rack/debugger.rb b/railties/lib/rails/rack/debugger.rb
new file mode 100644
index 0000000000..f7b77bcb3b
--- /dev/null
+++ b/railties/lib/rails/rack/debugger.rb
@@ -0,0 +1,24 @@
+module Rails
+ module Rack
+ class Debugger
+ def initialize(app)
+ @app = app
+
+ ARGV.clear # clear ARGV so that rails server options aren't passed to IRB
+
+ require 'debugger'
+
+ ::Debugger.start
+ ::Debugger.settings[:autoeval] = true if ::Debugger.respond_to?(:settings)
+ puts "=> Debugger enabled"
+ rescue LoadError
+ puts "You're missing the 'debugger' gem. Add it to your Gemfile, bundle it and try again."
+ exit(1)
+ end
+
+ def call(env)
+ @app.call(env)
+ end
+ end
+ end
+end
diff --git a/railties/lib/rails/rack/log_tailer.rb b/railties/lib/rails/rack/log_tailer.rb
new file mode 100644
index 0000000000..bc26421a9e
--- /dev/null
+++ b/railties/lib/rails/rack/log_tailer.rb
@@ -0,0 +1,38 @@
+require 'active_support/deprecation'
+
+module Rails
+ module Rack
+ class LogTailer
+ def initialize(app, log = nil)
+ ActiveSupport::Deprecation.warn "LogTailer is deprecated and will be removed on Rails 5"
+
+ @app = app
+
+ path = Pathname.new(log || "#{::File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
+
+ @cursor = @file = nil
+ if ::File.exist?(path)
+ @cursor = ::File.size(path)
+ @file = ::File.open(path, 'r')
+ end
+ end
+
+ def call(env)
+ response = @app.call(env)
+ tail!
+ response
+ end
+
+ def tail!
+ return unless @cursor
+ @file.seek @cursor
+
+ unless @file.eof?
+ contents = @file.read
+ @cursor = @file.tell
+ $stdout.print contents
+ end
+ end
+ end
+ end
+end
diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb
new file mode 100644
index 0000000000..9962e6d943
--- /dev/null
+++ b/railties/lib/rails/rack/logger.rb
@@ -0,0 +1,86 @@
+require 'active_support/core_ext/time/conversions'
+require 'active_support/core_ext/object/blank'
+require 'active_support/log_subscriber'
+require 'action_dispatch/http/request'
+require 'rack/body_proxy'
+
+module Rails
+ module Rack
+ # Sets log tags, logs the request, calls the app, and flushes the logs.
+ class Logger < ActiveSupport::LogSubscriber
+ def initialize(app, taggers = nil)
+ @app = app
+ @taggers = taggers || []
+ end
+
+ def call(env)
+ request = ActionDispatch::Request.new(env)
+
+ if logger.respond_to?(:tagged)
+ logger.tagged(compute_tags(request)) { call_app(request, env) }
+ else
+ call_app(request, env)
+ end
+ end
+
+ protected
+
+ def call_app(request, env)
+ # Put some space between requests in development logs.
+ if development?
+ logger.debug ''
+ logger.debug ''
+ end
+
+ instrumenter = ActiveSupport::Notifications.instrumenter
+ instrumenter.start 'request.action_dispatch', request: request
+ logger.info { started_request_message(request) }
+ resp = @app.call(env)
+ resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) }
+ resp
+ rescue Exception
+ finish(request)
+ raise
+ ensure
+ ActiveSupport::LogSubscriber.flush_all!
+ end
+
+ # 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
+
+ def compute_tags(request)
+ @taggers.collect do |tag|
+ case tag
+ when Proc
+ tag.call(request)
+ when Symbol
+ request.send(tag)
+ else
+ tag
+ end
+ end
+ end
+
+ private
+
+ def finish(request)
+ instrumenter = ActiveSupport::Notifications.instrumenter
+ instrumenter.finish 'request.action_dispatch', request: request
+ end
+
+ def development?
+ Rails.env.development?
+ end
+
+ def logger
+ Rails.logger
+ end
+ end
+ end
+end