aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-03-28 01:57:09 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-04-10 23:10:37 +0900
commit2b58ffe125db39028bfe829eda27708a462dfeb6 (patch)
tree6b10f70976c1b3df074bbac7ce0a27aff5de62c1
parent21992b6cbd83ba8a213f0d7a08bdf9c89e785113 (diff)
downloadruby-feature/logger-constructor-level.tar.gz
lib/logger.rb: allow specifying log level via Logger.newfeature/logger-constructor-level
* lib/logger.rb (initialize): Allow specifying log severity level in constructor. Logger.new(*args, level: lev) becomes effectively same as Logger.new(*args).tap { |l| l.level = lev }. * test/logger/test_logger.rb: Add test for this.
-rw-r--r--lib/logger.rb10
-rw-r--r--test/logger/test_logger.rb7
2 files changed, 13 insertions, 4 deletions
diff --git a/lib/logger.rb b/lib/logger.rb
index f3a29fa..63eece2 100644
--- a/lib/logger.rb
+++ b/lib/logger.rb
@@ -326,6 +326,7 @@ class Logger
# :call-seq:
# Logger.new(logdev, shift_age = 7, shift_size = 1048576)
# Logger.new(logdev, shift_age = 'weekly')
+ # Logger.new(logdev, level: :info)
#
# === Args
#
@@ -338,21 +339,22 @@ class Logger
# +shift_size+::
# Maximum logfile size (only applies when +shift_age+ is a number). Default
# value is 1MiB.
+ # +level+::
+ # The minimal severity level to log. Defaults to DEBUG.
#
# === Description
#
# Create an instance.
#
- def initialize(logdev, shift_age = 0, shift_size = 1048576)
+ def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG)
@progname = nil
- @level = DEBUG
@default_formatter = Formatter.new
@formatter = nil
@logdev = nil
if logdev
- @logdev = LogDevice.new(logdev, :shift_age => shift_age,
- :shift_size => shift_size)
+ @logdev = LogDevice.new(logdev, shift_age: shift_age, shift_size: shift_size)
end
+ self.level = level
end
#
diff --git a/test/logger/test_logger.rb b/test/logger/test_logger.rb
index 836d3b3..fea7aa0 100644
--- a/test/logger/test_logger.rb
+++ b/test/logger/test_logger.rb
@@ -292,4 +292,11 @@ class TestLogger < Test::Unit::TestCase
r.close
assert_equal("msg2\n\n", msg)
end
+
+ def test_initialize_specify_level
+ logger = Logger.new(STDERR) # default value
+ assert_equal(Logger::DEBUG, logger.level)
+ logger = Logger.new(STDERR, level: :info)
+ assert_equal(Logger::INFO, logger.level)
+ end
end