Skip to content

Commit

Permalink
Merge pull request #4686 from rmosolgo/fix-rails-without-logger
Browse files Browse the repository at this point in the history
Fix default_logger when Rails doesn't have a logger
  • Loading branch information
rmosolgo authored Oct 25, 2023
2 parents 013bed4 + d56efc2 commit fd867f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ def default_logger(new_default_logger = NOT_CONFIGURED)
@default_logger
elsif superclass.respond_to?(:default_logger)
superclass.default_logger
elsif defined?(Rails)
Rails.logger
elsif defined?(Rails) && Rails.respond_to?(:logger) && (rails_logger = Rails.logger)
rails_logger
else
def_logger = Logger.new($stdout)
def_logger.info! # It doesn't output debug info by default
Expand Down
28 changes: 28 additions & 0 deletions spec/graphql/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@
describe "Schema.default_logger" do
if defined?(Rails)
it "When Rails is present, returns the Rails logger" do
prev_logger = Rails.logger # might be `nil`
Rails.logger = Object.new
assert_equal Rails.logger, GraphQL::Schema.default_logger
ensure
Rails.logger = prev_logger
end

it "When Rails is present but the logger is nil, it returns a new logger" do
prev_logger = Rails.logger
Rails.logger = nil
refute_equal Rails.logger, GraphQL::Schema.default_logger
assert_instance_of Logger, GraphQL::Schema.default_logger
ensure
Rails.logger = prev_logger
end

else
it "Without Rails, returns a new logger" do
assert_instance_of Logger, GraphQL::Schema.default_logger
end

it "Works when Rails doesn't have a logger" do
rails_mod = Module.new
Object.const_set(:Rails, rails_mod)
assert_equal rails_mod, Rails
assert_instance_of Logger, GraphQL::Schema.default_logger

rails_mod.define_singleton_method(:logger) { false }
assert Rails.respond_to?(:logger)
assert_equal false, Rails.logger
assert_instance_of Logger, GraphQL::Schema.default_logger
ensure
Object.send :remove_const, :Rails
end
end

it "can be overridden" do
Expand Down

0 comments on commit fd867f7

Please sign in to comment.