TwP / logging-rails

Railtie for integrating the Logging framework with Rails
81 stars 27 forks source link

Issue with logging-rails when using a caching gem #7

Closed mixellent closed 9 years ago

mixellent commented 12 years ago

I got the following error when I try to run rails after having added the dalli gem (which is a popular gem for performing Caching on rails apps - https://github.com/mperham/dalli).

/Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/logging-rails-0.4.0/lib/logging/rails/railtie.rb:45:in `block in <class:Railtie>': undefined method `logger=' for #<ActiveSupport::Cache::DalliStore:0x007fcd1c0e6cd0> (NoMethodError)
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/michael/Projects/parcelio/config/environment.rb:5:in `<top (required)>'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/application.rb:83:in `require_environment!'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.1.3/lib/rails/commands.rb:39:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I checked and saw that it has something to do with the lib/logging/rails/ railtie.rb file:

initializer 'logging.initialize_cache', :after => 'initialize_cache' do
      ::Rails.cache.logger = ::Logging::Logger[::Rails.cache]
end

And here is what the dalli gem is doing with regards to logging:

  def self.logger
    @logger ||= (rails_logger || default_logger)
  end

  def self.rails_logger
    (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
    (defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
  end

  def self.default_logger
    require 'logger'
    l = Logger.new(STDOUT)
    l.level = Logger::INFO
    l
  end

  def self.logger=(logger)
    @logger = logger
  end

So I basically tried to remove the cache initializer inside your gem, on my fork, but I was unable to get the fork working on my rails app, since logging-rails doesn't have a .gemspec.

Could you maybe point out to a solution (if any is actually possible on your side?)

Thanks

TwP commented 12 years ago

It appears that the DaliStore cache object is not API compatible with the Rails caching layer. If you look at ActiveSupport::Cache::Store it defines logger accessor methods at the class level. Whatever object is assigned to be the ::Rails.cache needs to support these accessor methods.

I would open an issue on the dali gem instead.

ryanlchan commented 12 years ago

Just FYI, I submitted a pull request to Dalli to fix this issue. Mike just accepted, so this should no longer be an issue.