From a0409533866991529136224b549f53f2ab61c8e4 Mon Sep 17 00:00:00 2001 From: sonots Date: Mon, 18 Apr 2016 10:45:40 +0000 Subject: [PATCH] Allow specifying logger parameters in constructor * lib/logger.rb: Allow specifying logger prameters such as level, progname, datetime_format, formatter in constructor [Bug #12224] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54638 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/logger.rb | 38 ++++++++++++++++++++++++++++++---- test/logger/test_logger.rb | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/lib/logger.rb b/lib/logger.rb index f3a29fa47d..dc3d748dbc 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -184,6 +184,12 @@ require 'monitor' # # # :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' # 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' # 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 # :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 # +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, diff --git a/test/logger/test_logger.rb b/test/logger/test_logger.rb index 836d3b30f4..ce3952352c 100644 --- a/test/logger/test_logger.rb +++ b/test/logger/test_logger.rb @@ -166,6 +166,48 @@ class TestLogger < Test::Unit::TestCase 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)