ruby-changes:42564
From: sonots <ko1@a...>
Date: Mon, 18 Apr 2016 18:49:05 +0900 (JST)
Subject: [ruby-changes:42564] sonots:r54638 (trunk): Allow specifying logger parameters in constructor
sonots 2016-04-18 19:45:40 +0900 (Mon, 18 Apr 2016) New Revision: 54638 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54638 Log: Allow specifying logger parameters in constructor * lib/logger.rb: Allow specifying logger prameters such as level, progname, datetime_format, formatter in constructor [Bug #12224] Modified files: trunk/lib/logger.rb trunk/test/logger/test_logger.rb Index: lib/logger.rb =================================================================== --- lib/logger.rb (revision 54637) +++ lib/logger.rb (revision 54638) @@ -184,6 +184,12 @@ require 'monitor' https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L184 # # # :debug < :info < :warn < :error < :fatal < :unknown # +# 4. Constructor +# +# Logger.new(logdev, level: Logger::INFO) +# Logger.new(logdev, level: :info) +# Logger.new(logdev, level: 'INFO') +# # == Format # # Log messages are rendered in the output stream in a certain format by @@ -200,6 +206,10 @@ require 'monitor' https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L206 # logger.datetime_format = '%Y-%m-%d %H:%M:%S' # # e.g. "2004-01-03 00:54:26" # +# or via the constructor. +# +# Logger.new(logdev, datetime_format: '%Y-%m-%d %H:%M:%S') +# # Or, you may change the overall format via the #formatter= method. # # logger.formatter = proc do |severity, datetime, progname, msg| @@ -207,6 +217,12 @@ require 'monitor' https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L217 # end # # e.g. "2005-09-22 08:51:08 +0900: hello world" # +# or via the constructor. +# +# Logger.new(logdev, formatter: proc {|severity, datetime, progname, msg| +# "#{datetime}: #{msg}\n" +# }) +# class Logger VERSION = "1.2.7" _, name, rev = %w$Id$ @@ -326,6 +342,10 @@ class Logger https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L342 # :call-seq: # Logger.new(logdev, shift_age = 7, shift_size = 1048576) # Logger.new(logdev, shift_age = 'weekly') + # Logger.new(logdev, level: :info) + # Logger.new(logdev, progname: 'progname') + # Logger.new(logdev, formatter: formatter) + # Logger.new(logdev, datetime_format: '%Y-%m-%d %H:%M:%S') # # === Args # @@ -338,16 +358,26 @@ class Logger https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L358 # +shift_size+:: # Maximum logfile size (only applies when +shift_age+ is a number). Default # value is 1MiB. + # +level+:: + # Logging severity threshold. Default values is Logger::DEBUG. + # +progname+:: + # Program name to include in log messages. Default value is nil. + # +formatter+:: + # Logging formatter. Default values is an instance of Logger::Formatter. + # +datetime_format+:: + # Date and time format. Default value is '%Y-%m-%d %H:%M:%S'. # # === Description # # Create an instance. # - def initialize(logdev, shift_age = 0, shift_size = 1048576) - @progname = nil - @level = DEBUG + def initialize(logdev, shift_age = 0, shift_size = 1048576, level: DEBUG, + progname: nil, formatter: nil, datetime_format: nil) + self.level = level + self.progname = progname @default_formatter = Formatter.new - @formatter = nil + self.datetime_format = datetime_format + self.formatter = formatter @logdev = nil if logdev @logdev = LogDevice.new(logdev, :shift_age => shift_age, Index: test/logger/test_logger.rb =================================================================== --- test/logger/test_logger.rb (revision 54637) +++ test/logger/test_logger.rb (revision 54638) @@ -166,6 +166,48 @@ class TestLogger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L166 assert_nil(logger.datetime_format) end + def test_initialize_with_level + # default + logger = Logger.new(STDERR) + assert_equal(Logger::DEBUG, logger.level) + # config + logger = Logger.new(STDERR, level: :info) + assert_equal(Logger::INFO, logger.level) + end + + def test_initialize_with_progname + # default + logger = Logger.new(STDERR) + assert_equal(nil, logger.progname) + # config + logger = Logger.new(STDERR, progname: :progname) + assert_equal(:progname, logger.progname) + end + + def test_initialize_with_formatter + # default + logger = Logger.new(STDERR) + log = log(logger, :info, "foo") + assert_equal("foo\n", log.msg) + # config + logger = Logger.new(STDERR, formatter: proc { |severity, timestamp, progname, msg| + "#{severity}:#{msg}\n\n" + }) + line = log_raw(logger, :info, "foo") + assert_equal("INFO:foo\n\n", line) + end + + def test_initialize_with_datetime_format + # default + logger = Logger.new(STDERR) + log = log_add(logger, INFO, "foo") + assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+ $/, log.datetime) + # config + logger = Logger.new(STDERR, datetime_format: "%d%b%Y@%H:%M:%S") + log = log_add(logger, INFO, "foo") + assert_match(/^\d\d\w\w\w\d\d\d\d@\d\d:\d\d:\d\d$/, log.datetime) + end + def test_reopen logger = Logger.new(STDERR) logger.reopen(STDOUT) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/