ruby-changes:40542
From: sonots <ko1@a...>
Date: Tue, 17 Nov 2015 23:15:53 +0900 (JST)
Subject: [ruby-changes:40542] sonots:r52621 (trunk): * lib/logger.rb: support symbol and string log level setting
sonots 2015-11-17 23:15:25 +0900 (Tue, 17 Nov 2015) New Revision: 52621 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52621 Log: * lib/logger.rb: support symbol and string log level setting [fix GH-1101] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/logger.rb trunk/test/logger/test_logger.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 52620) +++ ChangeLog (revision 52621) @@ -176,6 +176,10 @@ Mon Nov 16 15:42:36 2015 Akinori MUSHA https://github.com/ruby/ruby/blob/trunk/ChangeLog#L176 * lib/set.rb (#>=, #>, #<=, #<): Make use of Hash#>=, #>, #<, and #<= when comparing against an instance of the same kind. +Mon Nov 16 15:37:11 2015 Naotoshi Seo <sonots@g...> + + * lib/logger.rb: Support symbol and string log level setting + Mon Nov 16 15:33:11 2015 SHIBATA Hiroshi <hsbt@r...> * tool/rbinstall.rb: fix wrong permission for gem specification without Index: lib/logger.rb =================================================================== --- lib/logger.rb (revision 52620) +++ lib/logger.rb (revision 52621) @@ -176,6 +176,13 @@ require 'monitor' https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L176 # # # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN # +# 3. Symbol or String (case insensitive) +# +# logger.level = :info +# logger.level = 'INFO' +# +# # :debug < :info < :warn < :error < :fatal < :unknown +# # == Format # # Log messages are rendered in the output stream in a certain format by @@ -234,7 +241,34 @@ class Logger https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L241 include Severity # Logging severity threshold (e.g. <tt>Logger::INFO</tt>). - attr_accessor :level + attr_reader :level + + # Set logging severity threshold. + # + # +severity+:: The Severity of the log message. + def level=(severity) + if severity.is_a?(Integer) + @level = severity + else + _severity = severity.to_s.downcase + case _severity + when 'debug'.freeze + @level = DEBUG + when 'info'.freeze + @level = INFO + when 'warn'.freeze + @level = WARN + when 'error'.freeze + @level = ERROR + when 'fatal'.freeze + @level = FATAL + when 'unknown'.freeze + @level = UNKNOWN + else + raise ArgumentError, "invalid log level: #{severity}" + end + end + end # Program name to include in log messages. attr_accessor :progname Index: NEWS =================================================================== --- NEWS (revision 52620) +++ NEWS (revision 52621) @@ -78,6 +78,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L78 what the enumerator has returned instead of nil. [Feature #11498] * Logger + + * Logger#level= now supports symbol and string levels such as :debug, :info, + :warn, :error, :fatal (case insensitive) [Feature #11695] * Logger#reopen is added to reopen a log device. [Feature #11696] * Module Index: test/logger/test_logger.rb =================================================================== --- test/logger/test_logger.rb (revision 52620) +++ test/logger/test_logger.rb (revision 52621) @@ -69,6 +69,50 @@ class TestLogger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L69 assert(!@logger.fatal?) end + def test_symbol_level + logger_symbol_levels = { + debug: DEBUG, + info: INFO, + warn: WARN, + error: ERROR, + fatal: FATAL, + unknown: UNKNOWN, + DEBUG: DEBUG, + INFO: INFO, + WARN: WARN, + ERROR: ERROR, + FATAL: FATAL, + UNKNOWN: UNKNOWN, + } + logger_symbol_levels.each do |symbol, level| + @logger.level = symbol + assert(@logger.level == level) + end + assert_raise(ArgumentError) { @logger.level = :something_wrong } + end + + def test_string_level + logger_string_levels = { + 'debug' => DEBUG, + 'info' => INFO, + 'warn' => WARN, + 'error' => ERROR, + 'fatal' => FATAL, + 'unknown' => UNKNOWN, + 'DEBUG' => DEBUG, + 'INFO' => INFO, + 'WARN' => WARN, + 'ERROR' => ERROR, + 'FATAL' => FATAL, + 'UNKNOWN' => UNKNOWN, + } + logger_string_levels.each do |string, level| + @logger.level = string + assert(@logger.level == level) + end + assert_raise(ArgumentError) { @logger.level = 'something_wrong' } + end + def test_progname assert_nil(@logger.progname) @logger.progname = "name" -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/