diff options
author | Carlhuda <carlhuda@engineyard.com> | 2010-03-02 13:05:25 -0800 |
---|---|---|
committer | Carlhuda <carlhuda@engineyard.com> | 2010-03-02 13:05:25 -0800 |
commit | c2dbc391a9292e6f73cadce2f0ba1be871b29e82 (patch) | |
tree | 3956dbc83c4d87a2e889017fd14883e1ef6285da /railties/lib/rails/log_subscriber.rb | |
parent | d434c5406846fb280b8a9d6ec40247b1f1b464c6 (diff) | |
download | rails-c2dbc391a9292e6f73cadce2f0ba1be871b29e82.tar.gz rails-c2dbc391a9292e6f73cadce2f0ba1be871b29e82.tar.bz2 rails-c2dbc391a9292e6f73cadce2f0ba1be871b29e82.zip |
Have log subscribers subscribe to the actual events, so the subscriber doesn't subscribe to *every* event, so we can have events that are slow-ish but are not actually run in production.
Diffstat (limited to 'railties/lib/rails/log_subscriber.rb')
-rw-r--r-- | railties/lib/rails/log_subscriber.rb | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/railties/lib/rails/log_subscriber.rb b/railties/lib/rails/log_subscriber.rb index 05cb70690a..0fbc19d89c 100644 --- a/railties/lib/rails/log_subscriber.rb +++ b/railties/lib/rails/log_subscriber.rb @@ -29,7 +29,7 @@ module Rails # This is useful because it avoids spanning several log subscribers just for logging # purposes(which slows down the main thread). Besides of providing a centralized # facility on top of Rails.logger. - # + # # Log subscriber also has some helpers to deal with logging and automatically flushes # all logs when the request finishes (via action_dispatch.callback notification). class LogSubscriber @@ -50,31 +50,29 @@ module Rails CYAN = "\e[36m" WHITE = "\e[37m" - def self.add(namespace, log_subscriber) - log_subscribers[namespace.to_sym] = log_subscriber - end - - def self.log_subscribers - @log_subscribers ||= {} - end + def self.add(namespace, log_subscriber, notifier = ActiveSupport::Notifications) + log_subscribers << log_subscriber - def self.dispatch(args) - namespace, name = args[0].split(".") - return unless namespace && name + log_subscriber.public_methods(false).each do |event| + notifier.subscribe("#{namespace}.#{event}") do |*args| + next if log_subscriber.logger.nil? - log_subscriber = log_subscribers[namespace.to_sym] - if log_subscriber.respond_to?(name) && log_subscriber.logger - begin - log_subscriber.send(name, ActiveSupport::Notifications::Event.new(*args)) - rescue Exception => e - Rails.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}" + begin + log_subscriber.send(event, ActiveSupport::Notifications::Event.new(*args)) + rescue Exception => e + Rails.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}" + end end end end + def self.log_subscribers + @log_subscribers ||= [] + end + # Flush all log_subscribers' logger. def self.flush_all! - loggers = log_subscribers.values.map(&:logger) + loggers = log_subscribers.map(&:logger) loggers.uniq! loggers.each { |l| l.flush if l.respond_to?(:flush) } end |