mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
ec2f63167e
commit
a040953386
2 changed files with 76 additions and 4 deletions
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue