提交 c83d9a11 编写于 作者: J José Valim

Unify logger and taggedlogging middleware as both address logging concerns.

上级 6c126015
*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]
......
......@@ -165,8 +165,7 @@ def default_middleware_stack
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?
......
......@@ -3,6 +3,5 @@ module Rack
autoload :Debugger, "rails/rack/debugger"
autoload :Logger, "rails/rack/logger"
autoload :LogTailer, "rails/rack/log_tailer"
autoload :TaggedLogging, "rails/rack/tagged_logging"
end
end
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
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册