Fix #401: use a Railtie to set Hashie.logger on rails boot.

This commit is contained in:
Matthew Rudy Jacobs 2017-02-10 18:24:03 -05:00 committed by dblock
parent 74b3bd6ab6
commit c269a1cc98
6 changed files with 21 additions and 10 deletions

View File

@ -28,6 +28,7 @@ scheme are considered to be bugs.
### Fixed
* [#402](https://github.com/intridea/hashie/pull/402): Use a Railtie to set Hashie.logger on rails boot - [@matthewrudy](https://github.com/matthewrudy).
* Your contribution here.
### Security

View File

@ -56,4 +56,6 @@ module Hashie
include Hashie::Extensions::StringifyKeys::ClassMethods
include Hashie::Extensions::SymbolizeKeys::ClassMethods
end
require 'hashie/railtie' if defined?(::Rails)
end

View File

@ -5,12 +5,7 @@ module Hashie
#
# @return [Logger]
def self.logger
@logger ||=
if defined?(::Rails)
Rails.logger
else
Logger.new(STDOUT)
end
@logger ||= Logger.new(STDOUT)
end
# Sets the logger that Hashie uses for reporting errors.

10
lib/hashie/railtie.rb Normal file
View File

@ -0,0 +1,10 @@
require 'rails/railtie'
module Hashie
class Railtie < Rails::Railtie
# Set the Hashie.logger to use Rails.logger when used with rails.
initializer 'hashie.configure_logger', after: 'initialize_logger' do
Hashie.logger = Rails.logger
end
end
end

View File

@ -62,9 +62,10 @@ Rails.application.config.middleware.use OmniAuth::Builder do
provider :some_site
end
RailsApp::Application.initialize!
# the order is important
# hashie must be loaded first to register the railtie
require 'hashie'
RailsApp::Application.initialize!
RSpec.describe 'the Hashie logger' do
it 'is set to the Rails logger' do

View File

@ -55,9 +55,11 @@ class ApplicationController < ActionController::Base
end
end
RailsApp::Application.initialize!
# the order is important
# hashie must be loaded first to register the railtie
# then we can initialize
require 'hashie'
RailsApp::Application.initialize!
RSpec.describe 'the Hashie logger' do
it 'is set to the Rails logger' do