lib/timber/frameworks/rails.rb in timber-1.1.14 vs lib/timber/frameworks/rails.rb in timber-2.0.0

- old
+ new

@@ -2,22 +2,47 @@ module Frameworks module Rails # Installs Timber into your Rails app automatically. class Railtie < ::Rails::Railtie config.timber = Config.instance - config.before_initialize do - Probes.insert! - Timber::Frameworks::Rails.insert_middlewares(config.app_middleware) + + initializer(:timber_logger, before: :initialize_logger) do + Rails.configure_middlewares(config.app_middleware) + Integrations.integrate! + + # We set a default logger because Rails tries to write to a file by default. + # This causes errors on paltforms with a readon only file system (Heroku) + # Moreover, the Timber logger gets configured properly later in an initiailizer. + # This is a hold over until we reach that file in the initialization process. + logger = if defined?(::ActiveSupport::Logger) + ::ActiveSupport::Logger.new(STDOUT) + else + ::Logger.new(STDOUT) + end + ::Rails.logger = Config.instance.logger = logger end end - def self.insert_middlewares(middleware) + def self.set_logger(logger) + if defined?(::ActiveSupport::TaggedLogging) && !logger.is_a?(::ActiveSupport::TaggedLogging) + logger = ::ActiveSupport::TaggedLogging.new(logger) + end + + Config.instance.logger = logger + ::ActionController::Base.logger = logger + ::ActionView::Base.logger = logger if ::ActionView::Base.respond_to?(:logger=) + ::ActiveRecord::Base.logger = logger + ::Rails.logger = logger + end + + def self.configure_middlewares(middleware) var_name = :"@_timber_middlewares_inserted" return true if middleware.instance_variable_defined?(var_name) && middleware.instance_variable_get(var_name) == true + # Rails uses a proxy :/, so we need to do this instance variable hack middleware.instance_variable_set(var_name, true) - Timber::RackMiddlewares.middlewares.each do |m| - middleware.insert_before ::Rails::Rack::Logger, m + Integrations::Rack.middlewares.each do |middleware_class| + middleware.use middleware_class end end end end end \ No newline at end of file