From 4b84f0fc76b72994c07eaaa5d1d6e8d6b19634c9 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Wed, 4 Sep 2019 17:55:42 -0400 Subject: [PATCH] Honor Logger#level overrides in Logger#add --- lib/logger.rb | 12 ++++++------ test/logger/test_logger.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/logger.rb b/lib/logger.rb index ddd80176df..0014165ea9 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -304,35 +304,35 @@ def datetime_format # Returns +true+ iff the current severity level allows for the printing of # +DEBUG+ messages. - def debug?; @level <= DEBUG; end + def debug?; level <= DEBUG; end # Sets the severity to DEBUG. def debug!; self.level = DEBUG; end # Returns +true+ iff the current severity level allows for the printing of # +INFO+ messages. - def info?; @level <= INFO; end + def info?; level <= INFO; end # Sets the severity to INFO. def info!; self.level = INFO; end # Returns +true+ iff the current severity level allows for the printing of # +WARN+ messages. - def warn?; @level <= WARN; end + def warn?; level <= WARN; end # Sets the severity to WARN. def warn!; self.level = WARN; end # Returns +true+ iff the current severity level allows for the printing of # +ERROR+ messages. - def error?; @level <= ERROR; end + def error?; level <= ERROR; end # Sets the severity to ERROR. def error!; self.level = ERROR; end # Returns +true+ iff the current severity level allows for the printing of # +FATAL+ messages. - def fatal?; @level <= FATAL; end + def fatal?; level <= FATAL; end # Sets the severity to FATAL. def fatal!; self.level = FATAL; end @@ -456,7 +456,7 @@ def reopen(logdev = nil) # def add(severity, message = nil, progname = nil) severity ||= UNKNOWN - if @logdev.nil? or severity < @level + if @logdev.nil? or severity < level return true end if progname.nil? diff --git a/test/logger/test_logger.rb b/test/logger/test_logger.rb index da68e58175..521b5627d4 100644 --- a/test/logger/test_logger.rb +++ b/test/logger/test_logger.rb @@ -363,4 +363,19 @@ def test_lshift r.close assert_equal("msg2\n\n", msg) end + + class CustomLogger < Logger + def level + INFO + end + end + + def test_overriding_level + logger = CustomLogger.new(nil) + log = log(logger, :info) { "msg" } + assert_equal "msg\n", log.msg + # + log = log(logger, :debug) { "msg" } + assert_nil log.msg + end end -- 2.17.1