Skip to content

Commit

Permalink
Add ActiveModelSerializers.logger with default null device
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Aug 31, 2015
1 parent 9ff35da commit 005f71e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/active_model/serializer/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'rails/railtie'
module ActiveModel
class Railtie < Rails::Railtie
initializer 'active_model_serializers.logger' do
ActiveSupport.on_load(:action_controller) do
ActiveModelSerializers.logger = ActionController::Base.logger
end
end

initializer 'generators' do |app|
app.load_generators
require 'generators/serializer/resource_override'
Expand Down
11 changes: 8 additions & 3 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
require 'logger'
require 'active_model'
require "active_support/railtie"
require 'action_controller'
require "action_controller/railtie"
module ActiveModelSerializers
mattr_accessor :logger
self.logger = Rails.logger || Logger.new(IO::NULL)

module_function

def silence_warnings
Expand All @@ -11,9 +19,6 @@ def silence_warnings

end

require 'active_model'
require 'action_controller'

require 'active_model/serializer'
require 'active_model/serializable_resource'
require 'active_model/serializer/version'
Expand Down
19 changes: 19 additions & 0 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class ActiveModelSerializers::LoggerTest < Minitest::Test

def test_logger_is_set_to_action_controller_logger_when_initializer_runs
assert_equal ActiveModelSerializers.logger, ActionController::Base.logger
end

def test_logger_can_be_set
original_logger = ActiveModelSerializers.logger
logger = Logger.new(STDOUT)

ActiveModelSerializers.logger = logger

assert_equal ActiveModelSerializers.logger, logger
ensure
ActiveModelSerializers.logger = original_logger
end
end

0 comments on commit 005f71e

Please sign in to comment.