1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/logger.rb (Logger): added formatter accessor to logger for

dictating the way in which the logger should format the messages it
          displays.  Thanks to Nicholas Seckar (cf. [ruby-talk:153391]) and
          Daniel Berger.

        * lib/logger.rb (Logger): added VERSION constant.

        * lib/logger.rb: removed document for LogDevice. It is an
          implementation detail and is not a public interface.

        * test/logger/test_logger.rb: added tests.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nahi 2005-09-13 13:13:41 +00:00
parent 9a7b502846
commit 5d0bf56235
3 changed files with 111 additions and 83 deletions

View file

@ -36,13 +36,17 @@ class TestLogger < Test::Unit::TestCase
end
def log(logger, msg_id, *arg, &block)
Log.new(log_raw(logger, msg_id, *arg, &block))
end
def log_raw(logger, msg_id, *arg, &block)
logdev = Tempfile.new(File.basename(__FILE__) + '.log')
logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
logger.__send__(msg_id, *arg, &block)
logdev.open
msg = logdev.read
logdev.close
Log.new(msg)
msg
end
def test_level
@ -98,6 +102,32 @@ class TestLogger < Test::Unit::TestCase
assert_match(/^$/, log.datetime)
end
def test_formatter
dummy = STDERR
logger = Logger.new(dummy)
# default
log = log(logger, :info, "foo")
assert_equal("foo\n", log.msg)
# config
logger.formatter = proc { |severity, timestamp, progname, msg|
"#{severity}:#{msg}\n\n"
}
line = log_raw(logger, :info, "foo")
assert_equal("INFO:foo\n\n", line)
# recover
logger.formatter = nil
log = log(logger, :info, "foo")
assert_equal("foo\n", log.msg)
# again
o = Object.new
def o.call(severity, timestamp, progname, msg)
"<<#{severity}-#{msg}>>\n"
end
logger.formatter = o
line = log_raw(logger, :info, "foo")
assert_equal("<<INFO-foo>>\n", line)
end
def test_initialize
logger = Logger.new(STDERR)
assert_nil(logger.progname)
@ -301,7 +331,6 @@ class TestLogDevice < Test::Unit::TestCase
assert(!File.exist?(logfile3))
logger.error("0" * 15)
assert(!File.exist?(logfile3))
logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)
@ -337,7 +366,6 @@ class TestLogDevice < Test::Unit::TestCase
assert(!File.exist?(logfile3))
logger.error("0" * 15)
assert(!File.exist?(logfile3))
logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)