Permalink
Browse files

Have log subscribers subscribe to the actual events, so the subscribe…

…r doesn't subscribe to *every* event, so we can have events that are slow-ish but are not actually run in production.
  • Loading branch information...
Carlhuda
Carlhuda committed Mar 2, 2010
1 parent d434c54 commit c2dbc391a9292e6f73cadce2f0ba1be871b29e82
@@ -5,7 +5,7 @@ class Application
class Configuration < ::Rails::Engine::Configuration
include ::Rails::Configuration::Deprecated
- attr_accessor :allow_concurrency, :cache_classes, :cache_store, :colorize_logging,
+ attr_accessor :allow_concurrency, :cache_classes, :cache_store,
:consider_all_requests_local, :dependency_loading,
:filter_parameters, :log_level, :logger, :metals,
:plugins, :preload_frameworks, :reload_engines, :reload_plugins,
@@ -14,7 +14,6 @@ class Configuration < ::Rails::Engine::Configuration
def initialize(*)
super
@allow_concurrency = false
- @colorize_logging = true
@filter_parameters = []
@dependency_loading = true
@serve_static_assets = true
@@ -81,6 +80,16 @@ def builtin_controller
def log_level
@log_level ||= Rails.env.production? ? :info : :debug
end
+
+ def colorize_logging
+ @colorize_logging
+ end
+
+ def colorize_logging=(val)
+ @colorize_logging = val
+ Rails::LogSubscriber.colorize_logging = val
+ self.generators.colorize_logging = val
+ end
end
end
end
@@ -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 @@ class LogSubscriber
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
@@ -43,7 +43,6 @@ def setup
@notifier = ActiveSupport::Notifications::Notifier.new(queue)
Rails::LogSubscriber.colorize_logging = false
- @notifier.subscribe { |*args| Rails::LogSubscriber.dispatch(args) }
set_logger(@logger)
ActiveSupport::Notifications.notifier = @notifier

0 comments on commit c2dbc39

Please sign in to comment.