Baron Bloomer 2021-08-17 11:57:45 +01:00 коммит произвёл Hiroshi SHIBATA
Родитель a8b11b5cdd
Коммит 4375546810
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: F9CF13417264FAC2
2 изменённых файлов: 38 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,35 @@
# coding: US-ASCII
# frozen_string_literal: false
require_relative 'helper'
class TestFormatter < Test::Unit::TestCase
def test_call
severity = 'INFO'
time = Time.now
progname = 'ruby'
msg = 'This is a test'
formatter = Logger::Formatter.new
result = formatter.call(severity, time, progname, msg)
time_matcher = /\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}/
matcher = /#{severity[0..0]}, \[#{time_matcher} #\d+\] #{severity} -- #{progname}: #{msg}\n/
assert_match(matcher, result)
end
class CustomFormatter < Logger::Formatter
def call(time)
format_datetime(time)
end
end
def test_format_datetime
time = Time.now
formatter = CustomFormatter.new
result = formatter.call(time)
matcher = /^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$/
assert_match(matcher, result)
end
end

Просмотреть файл

@ -13,7 +13,7 @@ class TestLogger < Test::Unit::TestCase
class Log
attr_reader :label, :datetime, :pid, :severity, :progname, :msg
def initialize(line)
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
/\A(\w+), \[([^#]*) #(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
end
end
@ -124,7 +124,7 @@ class TestLogger < Test::Unit::TestCase
dummy = STDERR
logger = Logger.new(dummy)
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)
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+$/, log.datetime)
logger.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)
@ -203,7 +203,7 @@ class TestLogger < Test::Unit::TestCase
# 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)
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")